ladies and gentlemen: so i was tryina write a program that will solve, or help me identify the 10 ensuing terms of this sequence: 3,7,11,... i did the arithmetic on paper and i end up with the program below: [3,7,11,] T=input("what are the next 10 numbers in the sequence?") print T ,the user input: map (lambda x : '4x-1' , range(11)) and this was the output:['4x-1', '4x-1', '4x-1', '4x-1', '4x-1', '4x-1', '4x-1', '4x-1', '4x-1', '4x-1', '4x-1'] i danno where did i go wrong, but if you could help, what must i do to get values?, in this format :[15,19,23,...]
...and oh! the program was written in python/pyscripter 2x vision.
you could use a for loop that prints out ten times starting at 15, adding 4 each iteration.
You put your calculation in quotes, which makes it into text.
when i do it without quotes, it gives me an error
it was pretty easy with the for loop, i just thought since map (lambda x:x*x,range(10)) works, that would work too. but how come that works and mine doesn't work?
Well, that error is your issue. So, what is the error for? How the lambda function is being done?
it's an syntax error only when i take the quotes off, and like i said, with the quotes i get ['4x-1', '4x-1', '4x-1', '4x-1', '4x-1', '4x-1', '4x-1', '4x-1', '4x-1', '4x-1', '4x-1']
OK. Syntax error. Well, why not do the lambda on another line so you can see if the syntax is in the lambda or in the map.
using the for loop, say i want the program to tell for every single number in the sequence whether it's even or odd, how do i do that? because what i did was : for i in range(3,14): X = 4*i -1 print x if x%2 == 0: print x, "is even" else: print x, "is odd"
I can't say I know the Python syntax to help you.. but in java you would start i out where you want to start testing, and have the boolean expression make the loop end whenever you want.. and for each iteration test for even/odd with the modulus operator- which it seems you already have that part; like ``` for(int i = 3; i <= 14; i++) { if(i%2 == 0) System.out.println(i + " Is even."); else System.out.println(i + " Is odd."); } ```
java ^
Just remember to define x outside the loop or it can not be used outside the loop.
and how do i exactly define x outside the loop?
In a strongly typed language with hard declarations it would be something like: int x In Python, which uses adaptive typing, you would do something like: x=0 Then it exists before the loop. Hmmm.... but looking at that code, I am not sure if you want to be out of the loop or not. It seems you may have a formatting problem caused by not using the code highlighting system. Did you mean this: ``` for i in range(3,14): x = 4*i -1 print x if x%2 == 0: print x, "is even" else: print x, "is odd" ``` or this: ``` for i in range(3,14): x = 4*i -1 print x if x%2 == 0: print x, "is even" else: print x, "is odd" ``` By putting ``` (the key with the ~ on it) on a separate line above and below the code, it keeps formatting and does code highlighting.
Join our real-time social learning platform and learn together with your friends!