Okay so ill apologize in advance if this question has previously been answered but I've looked thoroughly and cant seem to find anything that works. Im making a very simple game where you pretty much just have to guess a number between 1 and 1000 and if its incorrect the computer guesses a number either 1 above or below your guess. here is a function I've made to determine if the guess was too low
def numLow(userInput, low, high):
while userInput < num:
print ("The guess of {0} is low".format(userInput))
compGuess = (userInput + 1)
print ("My guess is {0}".format(compGuess))
low = (userInput + 1)
if compGuess < num:
print("The guess of {0} is low".format(compGuess))
userInput = int(input("Enter a value between {0} and
{1}:".format(low, high)))
else:
print("The guess of {0} is correct!".format(compGuess))
print("I WON!!!")
showTermination()
return (userInput, low)
now my issue is that i want to change the global variables userInput, low and high in the function. ive tried inserting
global userInput
global high
global low
before the function but it doesnt seem to work and if i put the globals inside the function i get "name 'userInput' is parameter and global". now im guessing the while loop is causing the problem but i cant seem to troubleshoot it. Im totally new to coding so i apologize if im breaking any coding rules or anything. Thanks for the help.
userInput for example is an input parameter to your function. The error message says exactly what the problem is here. You wanna use a global variable called userInput and you have an input parameter called userInput which are two different things for python. When userInput should be global then just define it somewhere globally like userInput = None and then instead of reaching it into the function as parameter just write in the function global userInput and python will know you are referencing to the globally instantiated variable. Both at the same time does not work.
Use globals() like this:
globals()['userInput'] = ...
Related
import random
input = input("Guess the number from 1 to 20 Computer is thinking: ")
if input.isdigit():
int(input)
else:
print("Error")
exit()
computer = int(random.randrange(1, 5))
print("Computer thinks of number", str(computer) + ".", end = " ")
if input == computer:
print("You Win!")
elif input != computer:
print("You Lost.")
Whenever I guessed the right number, It says that I lost. This is a simple project I made as a beginner so please explain in the simplest way possible
The key issue in your code is the variable not being stored.
In your provided picture here, you call int(input) to convert the input number into an integer, showing that you understand that input() returns a string (not many beginners know this!), however, you did not store the result returned by int() to a variable. In your case, it seems like you want to store it back to the input variable.
Because you did not store it, calling int(input) will not modify the value in the existing input variable, making it still string, and thus failing the comparison with the computer generated number which is an integer.
To fix this, simply replace the int(input) with input = int(input)
On a side note, it is not advisable to use variable names that are same as built-in functions, like input, or str, as doing so will override the built-in function, causing the subsequent calls to the input() function to return error, since now input is a variable, not a function anymore.
choice="y"
again="y"
coin=0
credit=0
allowed=[0,10,20,50,100,200]
def money_insert():
global again
global coin
global credit
global allowed
while again=="y":
try:
coin=int(input("insert coin"))
except:
print("thats not a coin")
while coin not in allowed:
print("invalid coin")
coin = 0
credit+=coin
again=input("another coin y/n?")
money_insert()
print("you have",credit,"p")
print("")
print("**********")
print("**1 coke 100p**")
print("*2 haribo 100p*")
print("*3 galaxy 100p*")
print("**4 mars 100p**")
print("*5 crisps 50p*")
selection=int(input("what would you like? 1-5"))
while choice=="y":
if selection==1:
if credit>99:
print("Here's your coke")
credit-=100
else:
print("not enough credit")
money_insert()
The last bit keeps displaying not enough credit continuously and I don’t know what to do
Sorry if this is a really dumb question I’m really new to python
choice is never altered, so you can never escape the while choice='y': loop. Then assuming the selection chosen was 1 you continue on to buying your coke. If you have previously entered coins via the money_insert function, presumably you would have at some point answered no to the question "another coin y/n?". You never reset the again variable, so subsequent calls to money_insert will just skip your for loop and not let you enter more money. You then basically follow the same path over and over again: while choice='y': → if selection==1: → if credit>99: ... else: → print("not enough credit")
I should also like to point out the issue with your money_insert function is a direct result of using a global variable where you don't need to. Global variables can be useful in certain instances, but they are generally frowned upon for cases such as this where you generally assume running a function with the same inputs will give the same result, but since the global variable has changed, the function now behaves differently. In this instance you don't need again to be global because it isn't used anywhere else, so you can move again='y' inside the function definition and remove the line global again to solve that particular issue. The same applies to coin and allowed as they aren't used anywhere but inside the function, and although they aren't causing a problem at the moment, leaving them available outside the function to be changed could cause a problem if you try to name something else by the same name somewhere else.
I thoroughly searched for an answer to my question but couldn't find anything that would explain my results. I truly hope that anyone of you can point me in the right direction.
At the moment I am trying to program a text-based adventure game using Python 3 in order to better understand the language.
While doing so I created a function that should ask the user for input and print a specific statement depending on the users input. In case the users input is invalid the function should then keep asking for input until it is valid.
Unfortunately the function only seems to keep asking for input, without ever executing the if/elif statements within the function. Due to no errors being shown I am currently at a loss as to why this is the case...
print("If You want to start the game, please enter 'start'." + "\n" +
"Otherwise please enter 'quit' in order to quit the game.")
startGame = True
def StartGame_int(answer):
if answer.lower() == "start":
startGame = False
return "Welcome to Vahlderia!"
elif answer.lower() == "quit":
startGame = False
return "Thank You for playing Vahlderia!" + "\n" + "You can now close
the window."
else:
return "Please enter either 'r' to start or 'q' to quit the game."
def StartGame():
answ = input("- ")
StartGame_int(answ)
while startGame == True:
StartGame()
You fell into the scoping trap: you are creating a new variable startGame inside the function that is discarded after you leave it. You would instead need to modify the global one:
def StartGame_int(answer):
global startGame # you need to specify that you want to modify the global var
# not create a same-named var in this scope
# rest of your code
This other SO questions might be of interest:
Python scoping rules
Asking the user for input until they give a valid response
Use of global keyword
and my all time favorite:
How to debug small programs (#1) so you enable yourself to debug your own code.
The last one will help you figure out why your texts that you return are not printed and why the if does not work on 'r' or 'q' and whatever other problems you stumble into. It will also show you that your if are indeed executed ;o)
Other great things to read for your text adventure to avoid other beginner traps:
How to copy or clone a list
How to parse a string to float or int
How to randomly select an item from a list
I have recently started to program in Python. Now im working on a number guessing game. I have a proplem with understanding how does storing a random number works.I have looked around here, found some answer but couldnt make it work. The code works fine, but my answer (random number) is always different, so its imposible to guess it.
If anybody could help me or improve my code i would be glad.
Here is the part of that code where i have it:
def game(self):
import random
answer = random.randint(0, 1000)
guess = int(input("Your tip is:"))
while True:
if guess < answer:
print ("Your tip is lower, than the answer! Try again.")
self.game()
elif guess > answer:
print ("Your tip is bigger than the answer! Try again.")
self.game()
elif guess == answer:
print ("Good job! You have found the answer!")
self.replay()
That's because you are calling self.game() inside each if statement, which brings the execution flow back to the start of the function and generates a new number each time with answer = random.randint(0, 1000).
So simply remove self.game() and let the function reach the end:
import random
def game(self):
answer = random.randint(0, 1000)
while True:
guess = int(input("Your tip is:"))
if guess < answer:
print ("Your tip is lower, than the answer! Try again.")
elif guess > answer:
print ("Your tip is bigger than the answer! Try again.")
elif guess == answer:
print ("Good job! You have found the answer!")
self.replay()
break
EDIT 1:
You should also move the line where the user inputs his guess inside the while loop so that the user can guess until he gets the correct answer. I also added break statements to exit the loop when he gets the correct answer instead of only print statements. You can read more about break here (answer code above now updated)
EDIT 2:
Another small detail since you're new to Python: you should place all your import statements at the top of your python module, it's python coding guidelines that you should follow to make your code more clear. You can read more here
I am very very new to Python and before this I only used extremely simple "programming" languages with labels and gotos. I am trying to make this code work in Sikuli:
http://i.imgur.com/gbtdMZF.png
Basically I want it to loop the if statement until any of the images is found, and if one of them is found, it executes the right function and starts looping again until it detects a new command.
I have been looking for tutorials and documentation, but I'm really lost. I think my mind is too busy trying to go from a goto/label to an organized programming language.
If someone could give me an example, I would appreciate it a lot!
In Python indentation matters, your code should look like this:
def function():
if condition1:
reaction1()
elif condition2:
reaction2()
else:
deafult_reaction()
I recommend reading the chapter about indentation in Dive Into Python as well as PEP 0008.
x = input( "please enter the variable")
print(" the Variable entered is " + x)
myfloat = int(x)
def compare (num):
if num%2 == 0:
print(" entered variable is even")
else:
print("entered variable is odd")
compare(myfloat)