Variable error in function [duplicate] - python

This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 5 years ago.
I wanted to make a number guesser game, and I wrote this code:
from random import randint as rand
number=rand(-1,1001)
tries=0
def numguess():
guess=int(input("The chosen number is between 0 to 1000.\nEnter your guess : "))
tries=tries+1
numguess()
while True:
if number==guess:
print ("You won. My number was effectively" ,number,". \n It took you ",tries, "to guess the number.")
break
elif number<guess:
print ("The number I chose is lower than your guess")
numguess()
else:
print ("The number I chose is higher than your guess")
numguess()
When I run it, it asks me for input then raises a UnboundLocalError. Is there something I am doing wrong? I tried searching but I don't understand. Thank you.

your tries variable is treated locally - since you assign value to it, use:
global tries
tries = tries +1
in your function

Related

How to write a Tic Tac Toe player input function in Python? [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Asking the user for input until they give a valid response
(22 answers)
Closed 2 years ago.
In the program I need to write a function that takes the player's input for where they want to place the 'X' or 'O' for the Tic Tac Toe game, which would be a number between 0-8 and the function would also recognise invalid responses. An invalid response would be a number not between 0-8, anything that is not a number or a number that has been inputted previously.
What is the problem?
No matter what the input is the function recognises it as invalid, even if it should be valid.
Here is the code
def playerinput():
print("Select the number on the board:")
while True:
playerchoice = input()
if playerchoice.isdigit == True:
if int(playerchoice) in validdata:
validdata.pop(int(playerchoice))
return int(playerchoice)
else:
print("That is not a valid input please enter a different value:")
else:
print("That is not a valid input please enter a different value:")
validdata is a global variable
validdata = [0,1,2,3,4,5,6,7,8]

Lists function not working in if condition [duplicate]

This question already has answers here:
Fastest way to check if a value exists in a list
(11 answers)
Closed 2 years ago.
Hi I am new to programming and I like to try to make the code work different way.
Unfortunately, variable called unlucky cant be read when printing.
So when the user enter the listed value [7 6 5], it doesnt print out as "You have selected unlucky number". I have include the image and copy of my code :
option = int(input("Guess the correct number between 0 to 10 : \nYour choice :"))
unlucky_number= [7,6,5] # Unlucky numbers, listed
if option == unlucky_number: # should print somewhere close when user enters list number
print("You have selected a unlucky number")
elif option== 6: # only 6 is correct
print ("Correct Guess")
elif option in range (0,4):
print("Not close")
else:
print ("Not in the range, choose between 1 to 10")
Please tell me whats wrong with it and how can I make a better version of it.
Thank you enter image description here
if option == unlucky_number
This line is causing you troubles. Your "option" holds a singular number, but your "unlucky_number" is a list. List can not be equal to a number, they are completely different animals. What you want is:
if option in unlucky_number

Why does this if statement not work correctly? [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
This is supposed to be a random number guessing game. It is supposed to say whether the number was guessed correctly or not at the end but it will always display "Wrong" even if the number has been guessed correctly.
I've only been learning python for a couple of days and this is doing my head in.
import random
odds = [1,2]
rNum=(random.choice(odds))
print ('The odds are:',odds)
gNum=input('What is your guess?')
print ('You Guessed:',gNum)
print ('The number was:',rNum)
if (rNum == gNum):
(print("Correct!"))
if (rNum != gNum):
(print("Wrong"))
Its as if it can't compare if the number is the same or not and always interprets it as wrong, as it only will give the != result
Forgive me if I sound foolish I am only beginning
Many thanks :)
I imagine the types are different. One is an int, the other a str which happens to contain an integer. Try"
gNum=int(input('What is your guess?'))

Triple nesting while loops Python 3.6.5 is not indefinite [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 4 years ago.
I am trying to do triple nesting of while loops.
If you input a decimal number it returns error, then if you input number above 31 it returns error, but if you try again to input decimal number code stops. Need help making it indefinite loop no matter how many times, or what order, a user inputs incorrect format. Also need to verify that dates input are valid for number of days in given month?
import string
varD= input("Enter Date/Day:")
while varD.isdigit() or varD.isspace()\
or varD.isdecimal or int(varD)>31 \
or int(varD)==26 or int(varD)<=0:
print ("Error: Enter Valid Number!")
varD= input("Enter Day:")
else:
print ("You have entered:", varD)
Use an infinite loop and break only when all criteria are satisfied instead.
while True:
varD = input("Enter Day:")
if varD.isdigit() and not varD.isspace() and varD.isdecimal() \
and int(varD) < 32 and int(varD) != 26 and int(varD) > 0:
break
print("Error: Enter Valid Number!")
print("You have entered: %s" % varD)
Also, your understanding of the term triple nesting is incorrect. Triple nesting means something like this:
while expression1:
while expression2:
while expression3:
do_something()

How do I make a type check function as a while loop? [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 7 years ago.
Ptarget = int(input("What is your target amount of points to achieve?"))
while Ptarget != int:
print("You have not provided a valid input, please try again.")
Ptarget = int(input("What is your target amount of points to achieve?"))
How do I make it so the while loop functions, by asking the user for another input if the previous was not an integer, without breaking the program?
Use an exception
while True:
try:
Ptarget = int(input("What is your target amount of points to achieve?"))
break
except:
print("You have not provided a valid input, please try again.")

Categories