Guessing a random integer

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
It keeps printing out this but my rand is defined in my def function:

NameError Traceback (most recent call last)
\ssfp\StuMydocs\belinda.garcia\My Documents\Computer Science\rough draft.py in ()
11 while on:
12 choose_max = input('Choose a max number for your range. ')
—> 13 print rand
14 choose_rannum = input('Choose a random number from 1 to ’ + str(choose_max)+ ’ ’ )
15 print choose_rannum

NameError: name ‘rand’ is not defined

<What do you expect to happen instead?>
So my goal isto guess a number from 1 to a max that the user chose. After they choose, I want it to go through the if, elif and else statments and if I do guess the number right I want to be able to go back and choose a new max number. I just want my code to work but obviously there’s something wrong with it. How should I alter my code to get it to work

```python

import random
nums_chosen =
def max_num(choose_max):
rand = random.randint(1,choose_max)
return rand
guessing = True
on = True
while on:
choose_max = input('Choose a max number for your range. ')
print rand
choose_rannum = input('Choose a random number from 1 to ’ + str(choose_max)+ ’ ’ )
print choose_rannum
nums_chosen.append(choose_rannum)
while guessing:
if choose_rannum == rand:
print(‘Awesome, you chose correctly.’)
play_again = raw_input('Would you like to play again? Type ‘Yes’ or ‘No’ ')
if play_again == ‘Yes’:
choose_max2 = input('Choose a new max for your range. ')
print choose_max2
new_max_num = max_num(choose_max2)

    else: 
        print('Thank you for playing')
        on = False
elif choose_rannum > choose_max or choose_rannum < 1: 
        print ('Your number is out of range. Try again.')
        print choose_rannum 
else: 
    if choose_rannum > rand: 
        print('Your number is too high. Try again.')
        print choose_rannum
    else: 
        print('Your number is too low. Try again.') 
        print choose_rannum

x = ('You guessed ’ + str(len(nums_chosen)) + ’ times. Here are the numbers you guessed: ’ + str(nums_chosen))
print x

<do not remove the three backticks above>

It looks like your second while should nested in the first so the on value means something.

Even when I put my second while it still says that my rand is not defined on lines 11, 16 and 30

It was easier to read your code as raw text. Please re-post. Thanks.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.