Jump to content
  • Advertisement
Sign in to follow this  
CodeTitan

Python Errors

This topic is 5104 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!