Sign in to follow this  

Python Errors

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I tried to write a simple random number guessing game in Python, but I get an error. Here is my source:
[source lang = "Python"]
import random

def guess():
    print "Welcome to the guessing game!\n"
    print "What do you want the maximum limit of the number to be?\n"
    max = raw_input()
    print "Enter your guess:"
    guess =  raw_input()
    number = random.randint(0, max)
    if guess == number:
        print "\nYou win!"
    else: 
        print "\nYou lose!"
guess()


I know that the error is from random.randint(0, max). What can I do to solve this error? EDIT: The error states that random.randint(0, max) can't concatenate str and int

Share this post


Link to post
Share on other sites
raw_input returns a string, which you then pass to randint. Try changing it to the following:
[source lang=python]
import random

def guess():
print "Welcome to the guessing game!\n"
print "What do you want the maximum limit of the number to be?\n"
max = raw_input()
print "Enter your guess:"

# the function int(...) converts a string to an integer, or
# throws an exception if it can't (help(int) for more
# information)
guess = int(raw_input())
number = random.randint(0, max)
if guess == number:
print "\nYou win!"
else:
print "\nYou lose!"

# make sure we're being run instead of being import'ed by another # module
if __name__ == "__main__":
guess()


Share this post


Link to post
Share on other sites
Your welcome :) It's nice to see people using python; it's a great language.
EDIT:
Also, here are some other conversion functions:
float(...) - converts a string or integer to a floating point number, or a number with decimals
str(...) - converts a number or an object to it's string representation.
repr(...) - converts a number or an object to it's string representation. I'm not sure how this is different from str(), maybe somebody can enlighten me?

Share this post


Link to post
Share on other sites
Objects conceptually have two "string representations" in Python. Both of course have a string type.

'str' is supposed to be a "friendly" representation, that looks the way you'd probably like it to if you're presenting it to the user.

'repr' is supposed to be a "practical" representation. The original idea apparently was to preserve the invariant

eval(repr(foo)) == foo

That is, the string produced by repr() could be evaluated as Python code to reproduce the original object (or a copy thereof). Of course, this doesn't work too well for a lot of objects.

You can compare the str and repr of various built-in objects rather easily at the command line:


>>> None # you see repr(None), which is completely empty.
>>> str(None) # actually repr(str(None), but repr of a string just puts it inside quotes and does escaping, so it looks like a string constant in code
'None'
>>> Ellipsis
Ellipsis
>>> str(Ellipsis) # Not so interesting.
'Ellipsis'
>>> str # Most types have similar str() and repr()
<type 'str'>
>>> str(str)
"<type 'str'>"
>>> int
<type 'int'>
>>> str(int)
"<type 'int'>"
>>> object()
<object object at 0x008AD3B8>
>>> str(object())
'<object object at 0x008AD3B8>'
>>> str(123L) # notice: the constant is of type long,
'123'
>>> 123L # and you can't determine that from the str(),
123L
>>> str(123) # but you can from the repr().
'123'
>>> 123
123

Share this post


Link to post
Share on other sites

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this