from random import randint
isRunning =True
while isRunning:
dice1 = randint(1,7)
dice2 = randint(1,7)
print("The first die landed on ℅d and the second landed on ℅d." ℅ (dice1,dice2))
user_input = input("Contiue? Yes or No?\n>")
if user_input == "Yes" or "yes":
print("="*16)
elif user_input == "No" or "no":
isRunning = False
I feel like I'm making such a simple mistake and when I decided to look into global variables and what not it still doesn't help. Could anyone explain why the while loop doesn't terminate, although the variable was set to false.
if user_input== "Yes" or "yes" should be
if user_input== "Yes" or user_input =="yes", alternatively it's equivalent to if any(user_input==keyword for keyword in["Yes", "yes"]):
your original if clause is splitting to
if user_input=="Yes" or if "yes" and if "yes" always true, therefore your if-elseif clause always go to if clause.
Related
I want to return the if statements if they equal the value of the dictionary and print the dictionary value not just use == 'HW' and print some text. Right Now I return nothing So I am wondering what I did wrong. How do I get out of the while loop when I give a wrong answer first and then a correct one?
from ex45f import live
class adult(live):
def __init__(self, choose):
self.choose = choose
def choices(self):
options = {
'HW': 'Hard Working',
'PA': 'Partying',
'DE': 'Doing everyting a bit',
}
#while choose != 'HW' or 'PA' or 'DE':
while not (self.choose == 'HW' or self.choose == 'PA' or self.choose == 'DE'):
x = input("""Choose again
> """)
print(x)
if self.choose == options['HW']:
return "You are going to be millionare"
elif self.choose == options['PA']:
return "You will have the first year a great life and then you will hate it"
elif self.choose == options['DE']:
return "Nothing intersting in life happens."
else:
return "Wrong input"
choose = input("""Choose one of those options: HW, PA, DE)
> """)
zw = adult(choose)
zw.choices()
So a couple of comments:
self.choose will either be HW, PA, or DE
your if statemen checks if self.choose == options['HW'].
options['HW'] is really "Hard Working", so in the case above self.choose will always end at the else.
Your if statements should be:
if self.choose == "HW":
if self.choose == "PA":
if self.choose == "DE":
Your while loop could look like: while self.choose not in options:
if self.choose is not in the list, you get a new input and store it in x. self.choose remains unchanged. Thus the while loop will be infinite.
instead of x = input do self.choose = input so when the user enters one of the correct options, they will leave the while loop.
If you want to get the value of options add return "{} The text to return".format(options[self.choose])
Finally, if you have no interest in the terms Hard Working, Partying, or Doing everthing a bit, then just make options a list:options = ["DE", "PA", "HW"] Then you do not need options[self.choose]
I hope that helped!
I have a question on how to check a user's input and make sure they are returning a specific string. Currently, the function when called will ask the user for their input. However, if they choose a string that is not part of the function, the else statement will execute and continue the code. I am trying to figure out how to loop this function, until a user inputs one of the strings that the function is looking for. Can anyone help me with this? I am new to python and would appreciate any help.
def dwarf_class_definer(dwarf_class):
if dwarf_class == "Thane":
print("'Ahhh Nobility'")
elif dwarf_class == "Mekanik":
print("'Interesting a Mechanic'")
elif dwarf_class == "Ancestrite":
print("'A spiritualist. I see...'")
elif dwarf_class == "Prisoner":
print("'Never met a gen-u-ine 'Last chancer.'")
elif dwarf_class == "Civilian":
print("'ehhh a civilian? Wut you doing here?'")
else:
print("You aren't choosing a valid class.")
dwarf_class = input("Which Class will you choose?: ")
dwarf_class_definer(dwarf_class)
A while loop will keep going until you tell it not to anymore. You can see when an expected value is supplied, the break command will terminate the while loop. A dictionary can also make your code a lot cleaner and easier to maintain compared to a bunch of if statements.
dwarf_classes = {
"Thane": "'Ahhh Nobility'",
"Mekanik": "'Interesting a Mechanic'",
"Ancestrite": "'A spiritualist. I see...'",
"Prisoner": "'Never met a gen-u-ine 'Last chancer.'",
"Civilian": "'ehhh a civilian? Wut you doing here?'",
}
while True:
dwarf_class = input("Which Class will you choose?: ")
if dwarf_class in dwarf_classes.keys():
print(dwarf_classes[dwarf_class])
break
print("You aren't choosing a valid class.")
example:
$ python3 so.py
Which Class will you choose?: Python!
You aren't choosing a valid class.
Which Class will you choose?: Prisoner
'Never met a gen-u-ine 'Last chancer.'
I need some help understanding the differences between the following. In the first example, I want the loop to break when the user inputs False:
true = True
while true:
print("Not broken")
true = input("to break loop enter 'False' ")
There was a question asked at:
how do I break infinite while loop with user input
Which gives this solution:
true= True
while true:
print("Not broken")
true = input("to break loop enter 'n' ")
if true == "n":
break
else:
continue
And I don't understand why the first method doesn't work and the second does??? Why doesn't python take the input as if someone was changing the script and change the variable "true"? Whats going on behind the scenes?
Any help would be appreciated. Thanks in advance :)
The while statement is conditional, and the user entering the String "False" will still resolve to a True outcome.
For an idea of what Python considers True and False, checkout this link: https://realpython.com/python-conditional-statements/
Building on this answer Converting from a string to boolean in Python?, the best way to check is:
true = True
while true is not 'False':
print("Not broken")
true = input("to break loop enter 'False' ")
I've got a simple piece of code that asks for user input and returns a boolean variable. In case the input was unacceptable, the user has the chance to correct herself. But the boolean gets properly updated only when the else part of the if-statement is not invoked. When it is, the function always returns False.
def tryAgain():
bol = False
print('Do you want to try again? (Y/N)')
answer = input('> ').lower()
if (answer == 'y' or answer == 'n'):
if answer == 'y':
bol = True
else:
print('Your answer could not be parsed')
tryAgain()
return bol
That line of
tryAgain()
should be
bol = tryAgain()
And it will work. :-)
Oops... as per what Saeed said... Hadnt read his comment before replying.
How do you reset a while True loop. For example this is my code:
x=True
while x==True:
print ("Random Text")
randomtext= input()
if randomtext == "yes":
print ("hi")
x=False
elif randomtext == "no":
print("This is supposed to reset the loop")
#resets loop and prints Random Text again
print ("hi")
if x==True:
print ("placeholder text")
input()
I want it to reset the loop if "randomtext" is equal to yes. I want to reset it it in the middle of the loop. This is a very simple question but this is getting in my programming. Thank you.
I'm assuming that by resetting the loop you mean jumping code until reaching the start of the loop again. This is done with the "continue" keyword.
if randomtext == "yes":
continue
If you actually meant breaking out of the loop, you should instead use the "break" keyword.