Please what's wrong with this code? I keeep getting User is not defined. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please what's wrong with this code? I keeep getting User is not defined.

# It has been corrected num = list(range(1, 20)) User = int(input("enter a number:")) for i in num: if User % 3 ==0: print("You got it") break elif User > 20: print("Almost there") break else: print("Try again") break

23rd Mar 2022, 9:32 AM
David Michael
David Michael - avatar
24 Answers
+ 6
Check that print() is not capitalised? I think all the python functions are lowercase or maybe camelCase. I also see that you have used range(1,20) That will give you a sequential list starting at 1 but finishing at 19. Is that what you wanted? The range() function takes 3 arguments, start, end, step. So range(2,8,3) would give 2, 5 but would not give the next step of 3 because it exceeds or is equal to the end value.
23rd Mar 2022, 9:47 AM
HungryTradie
HungryTradie - avatar
+ 4
G'day David Michael or should I say dAVID mICHAEL.... Capitalisation matters to a lot of programming languages, python included. your last print() statement is also incorrectly capitalised.
23rd Mar 2022, 9:36 AM
HungryTradie
HungryTradie - avatar
+ 4
Python is case sensitive: "User" and "user" are two different variables; Print() #bug
23rd Mar 2022, 9:46 AM
Solo
Solo - avatar
+ 2
Ooh, oh my God I just found it, thank you very much. I love this community
23rd Mar 2022, 9:38 AM
David Michael
David Michael - avatar
+ 2
No worries. Can you become a champion of the forum? Edit your original post to start with [Solved] https://www.sololearn.com/discuss/1316935/?ref=app
23rd Mar 2022, 9:39 AM
HungryTradie
HungryTradie - avatar
+ 2
Yea, it is what I wanted, I was just trying it out, I'm new to all of these. And I didn't really understand the range you explained, I think I'll check a video on it. Thank you very much, this is relly helpful
23rd Mar 2022, 9:50 AM
David Michael
David Michael - avatar
+ 2
Sorry about jamming extra information in, I should have left it at helping you to find rogue capital letters. 👍
23rd Mar 2022, 9:55 AM
HungryTradie
HungryTradie - avatar
+ 2
HungryTradie If I don't break, it'll print the reult multiple times
23rd Mar 2022, 10:59 AM
David Michael
David Michael - avatar
+ 2
Korkunç TheTerrible It is to check a number between 1 to 20 that is divisible by 3
23rd Mar 2022, 1:03 PM
David Michael
David Michael - avatar
+ 2
David Michael So you don't want a for loop, then? Why not just: n = int(input()) if n<20 and n%3 ==0: print("yay") else: print (n, "is not a multiple of 3 that is less than 20") If you had no idea about the logic operator "and", you could do: n = int(input()) if n % 3 ==0: if n < 20: print("yay") else: print("a mult of 3 less than 20 please") else: print(" the number you enter should be both a multiple of 3 and less than 20") I mean I don’t know, but if you want to do it once, neither the list nor the for loop is for that purpose. If you want to try all numbers within the list the breaks need to be removed. BTW: I am a noob, I just got confused
23rd Mar 2022, 1:20 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
I still have a problem, if i input a number greater than 20, I get an error message that print name is not defined, what do i do?
23rd Mar 2022, 9:41 AM
David Michael
David Michael - avatar
+ 1
Hungry Tradie Thank you very much, I just corrected that.
23rd Mar 2022, 9:44 AM
David Michael
David Michael - avatar
+ 1
No, you just gave me somethinn to learn today, am I'm on it now. Thank you, My account is not verified yet, I would've inbox
23rd Mar 2022, 9:59 AM
David Michael
David Michael - avatar
+ 1
HungryTradie I think there's a problem with that somehow,
23rd Mar 2022, 10:17 AM
David Michael
David Michael - avatar
23rd Mar 2022, 10:43 AM
David Michael
David Michael - avatar
+ 1
Ok, I'm sorry, but I don't understand the game. You have each option terminated by a break, so your for loop won't get another iteration. Don't you want one of them to continue the loop? Your success condition is always any multiple of 3, disregarding not more than 20. If it is how you want it, then that is great. Well done.
23rd Mar 2022, 10:54 AM
HungryTradie
HungryTradie - avatar
+ 1
You don't need to use loop then num = list(range(1, 20)) User = int(input("enter a number:")) if (): print() elif (): print() else: print()
23rd Mar 2022, 11:52 AM
Simba
Simba - avatar
+ 1
I am a learner too. May I ask what the purpose of this code is? It looks confusing to me.
23rd Mar 2022, 12:57 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
There are no error in this code. You could wrote it wrong in case means upper or lower.
23rd Mar 2022, 1:47 PM
Sanjoy Singha (āĻ¸āĻžā§āĻœā§Ÿ āĻ¸āĻŋāĻ‚āĻš)
Sanjoy Singha (āĻ¸āĻžā§āĻœā§Ÿ āĻ¸āĻŋāĻ‚āĻš) - avatar
+ 1
Yeah just like the first comment, I think you capitalized the variables wrongly in the print()
25th Mar 2022, 6:40 AM
James Anthony
James Anthony - avatar