+ 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
15 Answers
+ 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!")
+ 1
To follow the type of a variable you can use :
print( type(Guess) )
You will see that the type will be "int" everywhere.
+ 1
Thanks Guys,although i did try another approach
https://code.sololearn.com/cSu520tN6rN8/?ref=app
+ 1
Great. Now you could try to force users to type a number between 1 and 5 and refuse others values =)
0
your if should be "Guess == rnd_number not " pick number".
0
i tried changing to Guess still have same problem not working
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.
0
Thanks a bunch Geoffrey...but now i can't cartenate string and integer in "print (your number is + Guess)"
0
In my first post it works, I casted Guess as a string by using str()
0
Wouldn't raw_input do the same job as int(input("pick_number")) ?
0
Geoffrey L but i want "Guess" to be an integer not string
0
unoke solomon i guess it would
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.
0
Thanks, although just started couldn't believe i'd use close to 3hrs on just 10lines of code. phew!!!
0
^^ with some practice that will do it. Keep going !