New to python need random.randint HELP

This is the place for queries that don't fit in any of the other categories.

New to python need random.randint HELP

Postby mossymam » Sat Nov 30, 2013 10:35 am

Code: Select all
import random


def is_same(target, number):
    if target == number:
        result= "Win"
    elif target > number:
        result = "Low"
    else:
        result="High"
    return result




difficulty = input("What level do you want to play? Easy (e), Medium (m) or Hard (h)")
level= "10" and "20" and "30"
computernumber = random.randint(1,level)
if difficulty== "e":
    level= 10
    print("I am thinking of a number between 1 and 10")
elif difficulty== "m":
    print("I am thinking of a number between 1 and 20")
    level= 20
elif difficulty== "h":
    print("I am thinking of a number between 1 and 30")
    level= 30
while difficulty != "e" and difficulty != "m" and difficulty != "h":
    difficulty= input("Sorry. You must type in one of the letters 'e','m' or 'h'\ne/m/h:")




print("Hello. \nI Have thought of a number between 1 and 100.")

guess = int(input("Can you guess it?\n"))

correct_guess = is_same(computer_number, guess)


while correct_guess != "Win":

    if correct_guess == "Low":
        guess = int(input("Sorry you are too low. Try Again.\n"))
    else:
        guess = int(input("Sorry you are too high. Try again.\n"))

    correct_guess = is_same(computer_number, guess)



input("Correct!\nWelldone\n\nPress RETURN to exit")
Last edited by Mekire on Sat Nov 30, 2013 11:57 am, edited 1 time in total.
Reason: FIrst post lock. Code tags added. Please read "New Users Read This".
mossymam
 
Posts: 3
Joined: Sat Nov 30, 2013 10:31 am

Re: New to python need random.randint HELP

Postby Mekire » Sat Nov 30, 2013 11:58 am

Welcome to the forum.

Please tell us exactly what problem you are having. Also if you are receiving an error, please include the traceback in your post (in code tags).

-Mek
User avatar
Mekire
 
Posts: 828
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: New to python need random.randint HELP

Postby mossymam » Sat Nov 30, 2013 1:26 pm

thanks for your post
when i run the program and enter h i get this error msg


Code: Select all
What level do you want to play? Easy (e), Medium (m) or Hard (h)h
Traceback (most recent call last):
  File "C:\Users\Mark\Downloads\guessing_game improved.py", line 18, in <module>
    computernumber = random.randint(1,level)
  File "C:\Python33\lib\random.py", line 214, in randint
    return self.randrange(a, b+1)
TypeError: Can't convert 'int' object to str implicitly
Last edited by Mekire on Sat Nov 30, 2013 1:31 pm, edited 1 time in total.
Reason: Code tags.
mossymam
 
Posts: 3
Joined: Sat Nov 30, 2013 10:31 am

Re: New to python need random.randint HELP

Postby Mekire » Sat Nov 30, 2013 1:49 pm

The function random.randint takes two integers and returns you a random number in between them (inclusive of the endpoints).
You are currently passing it a string.

I'm not sure what you think this line does:
Code: Select all
level= "10" and "20" and "30"

What you wrote there is identically equivalent to:
Code: Select all
level="30"
Add a print statement after this line to verify this. Note that it is the string "30", not the integer 30. When you pass this string as the second argument to randint you get an exception; because randint accepts integers; not strings.

Since you are trying to take the letters e, m, and h I think a dictionary would suit you.

Take a look at this for starters:
Code: Select all
DIFFICULTY_SETTINGS = {"e":10, "m":20, "h":30}

difficulty = None
while difficulty not in DIFFICULTY_SETTINGS:
    if difficulty != None:
        print("Sorry. You must type in one of the letters 'e','m' or 'h'")
    prompt = "What level do you want to play? Easy (e), Medium (m) or Hard (h): "
    difficulty = input(prompt)

level = DIFFICULTY_SETTINGS[difficulty]
computer_number = random.randint(1,level)
print("I am thinking of a number between 1 and {}".format(level))

Hopefully this gets you back on track.
-Mek
User avatar
Mekire
 
Posts: 828
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: New to python need random.randint HELP

Postby mossymam » Sat Nov 30, 2013 2:18 pm

Thank you very much, that really helped me
mossymam
 
Posts: 3
Joined: Sat Nov 30, 2013 10:31 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 1 guest