+ 1

Hey guys I'm new to Python and need help with my code

The code doesn't display the if-statment whether its true or not https://code.sololearn.com/cSu520tN6rN8/?ref=app

20th Nov 2018, 9:31 PM
Tochukwu Udeozor
Tochukwu Udeozor - avatar
15 Respuestas
+ 1
Your if is wrong you compare a string with an integer. Guess == rnd_number Here is your corrected code : import random print ("Welcome To Game Home") rnd_number=random.randint(0,5) print (rnd_number) Guess=int(input("Pick_number\n")) print ("Your number is"+ str(Guess)) if Guess==rnd_number: print ("Congrats!!!") else: print ("wrong,Try again!")
20th Nov 2018, 9:33 PM
Geoffrey L
Geoffrey L - avatar
+ 1
To follow the type of a variable you can use : print( type(Guess) ) You will see that the type will be "int" everywhere.
20th Nov 2018, 9:56 PM
Geoffrey L
Geoffrey L - avatar
+ 1
Thanks Guys,although i did try another approach https://code.sololearn.com/cSu520tN6rN8/?ref=app
20th Nov 2018, 10:01 PM
Tochukwu Udeozor
Tochukwu Udeozor - avatar
+ 1
Great. Now you could try to force users to type a number between 1 and 5 and refuse others values =)
20th Nov 2018, 10:04 PM
Geoffrey L
Geoffrey L - avatar
0
your if should be "Guess == rnd_number not " pick number".
20th Nov 2018, 9:40 PM
unoke solomon
unoke solomon - avatar
0
i tried changing to Guess still have same problem not working
20th Nov 2018, 9:42 PM
Tochukwu Udeozor
Tochukwu Udeozor - avatar
0
I just edit my first answer, it works with int() around input. That's to force to declare the input has integer. If not your if will read it as a text.
20th Nov 2018, 9:42 PM
Geoffrey L
Geoffrey L - avatar
0
Thanks a bunch Geoffrey...but now i can't cartenate string and integer in "print (your number is + Guess)"
20th Nov 2018, 9:47 PM
Tochukwu Udeozor
Tochukwu Udeozor - avatar
0
In my first post it works, I casted Guess as a string by using str()
20th Nov 2018, 9:48 PM
Geoffrey L
Geoffrey L - avatar
0
Wouldn't raw_input do the same job as int(input("pick_number")) ?
20th Nov 2018, 9:50 PM
unoke solomon
unoke solomon - avatar
0
Geoffrey L but i want "Guess" to be an integer not string
20th Nov 2018, 9:53 PM
Tochukwu Udeozor
Tochukwu Udeozor - avatar
0
unoke solomon i guess it would
20th Nov 2018, 9:55 PM
Tochukwu Udeozor
Tochukwu Udeozor - avatar
0
And it still be, have you try my code ? When I cast Guess as a string by using str(), you can notice that Guess is not redefined inside a variable, I just change its type inside the print but not changing his type of variable in the whole code.
20th Nov 2018, 9:55 PM
Geoffrey L
Geoffrey L - avatar
0
Thanks, although just started couldn't believe i'd use close to 3hrs on just 10lines of code. phew!!!
20th Nov 2018, 10:07 PM
Tochukwu Udeozor
Tochukwu Udeozor - avatar
0
^^ with some practice that will do it. Keep going !
20th Nov 2018, 10:08 PM
Geoffrey L
Geoffrey L - avatar