Python Forum
Thread Rating:
  • 4 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
new user coding problem
#1
"""This is my code"""
def main():
    print ("Guess a number between 1 and 100.")
    randomNumber = 35
    found = False       # flag variable to see
                        # if they've guessed it
    while not found:
        userGuess = input("Your guess: ")
        if userGuess == randomNumber:
            print ("You got it!")
            found = True
        elif userGuess > randomNumber:
            print ("Guess lower!")
        else:
            print ("Guess higher!")

main()
""" and this was the error message"""
Error:
Traceback (most recent call last):   File "/Users/Matthew/Documents/new.py", line 19, in <module>     main()   File "/Users/Matthew/Documents/new.py", line 14, in main     elif userGuess > randomNumber: TypeError: '>' not supported between instances of 'str' and 'int' >>> 
I think I am following the video exactly. 

Can someone tell me precisely where and what the mistake is AND why it is a mistake?

Thanks.

Moderator:
As buran pointed out, please use code tags.
I added them for you...this time.
sparkz_alot
Reply
#2
First of all - please, use code tags when posting code and error tags, when posting traceback

when taking user input
userGuess = input("Your guess: ")
userGuess is of type str and you try to compare it with randomNumber which in this particular case is 35 (i.e. type int)
you need to convert userGuess to int in order to be able to compare it with variable of type int
userGuess = int(input("Your guess: "))
however note that in case user enter something that is not int, this will rise error. Eventually you need to handle this too.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Armachat coding problem gad969 4 1,312 Mar-12-2023, 03:12 PM
Last Post: gad969
  Coding problem portfolio grade max70990 1 738 Dec-11-2022, 12:30 PM
Last Post: Larz60+
  Problem restricting user input in my rock paper scissors game ashergreen 6 4,623 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  coding problem from older book teddfirth 3 2,148 Mar-06-2021, 03:51 PM
Last Post: teddfirth
  Problem with user defined main menu function stefzeer 3 2,411 Mar-27-2020, 06:12 AM
Last Post: buran
  Begginer coding problem wiktor 2 3,235 Jan-26-2018, 06:24 PM
Last Post: wiktor
  IR coding problem DPaul 3 2,778 Jan-09-2018, 08:02 AM
Last Post: DPaul
  I have a problem with user enter, any help? Qubayel 2 3,544 Mar-20-2017, 11:24 AM
Last Post: Qubayel
  Coding problem VISHU 3 4,834 Mar-16-2017, 06:54 AM
Last Post: Larz60+
  Incredibly basic coding problem AndyF 12 8,535 Feb-11-2017, 03:52 PM
Last Post: AndyF

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020