I randomly created this dice program that I need help with:
import random
number = random.randint(1,6)
print ("You got", number)
yes = input ("Would you like to roll again: yes or no")
while (yes != "no"):
number1 = random.randint(1,6)
if yes == ("yes"):
print ("You got", number1)
if yes == ("no"):
print ("Goodbye")
For some reason, whenever I type “yes”, it just gives me an endless list of numbers.
Can someone help?
The code inside a while loop will repeat while the condition you provide evaluates to truthy. Once control enters the loop, if there is no code that could possibly alter the the truthiness of the condition, you have an infinite loop.
So, I tried to fix it, but now, whenever I say no, it still gives me a number. Here is the program:
import random
number = random.randint(1,6)
print ("You got", number)
yes = input ("Would you like to roll again: yes or no")
while (yes != "no"):
number1 = random.randint(1,6)
if yes == ("yes"):
print ("You got", number1)
yes1 = input("Would you like to roll again: yes or no")
if yes == ("no"):
print ("Goodbye")
Control flow starts at the top (first line of code), and falls through to the bottom. Things that redirect control flow are things like loops, conditional statements and function calls. Place your finger on the first line of code, and execute each line moving down in you head, or write what happens on paper. I’ll give you a hand with this program, but being able to follow the flow yourself is key to writing programs. You’ve got to know in which order your code will be executed when you write it.
import random #imports the random module
number = random.randint(1,6) #assigns a randomly generated integer 1-6 to number
print ("You got", number) #this prints (so far, these lines execute in this order every time you run the program.)
yes = input ("Would you like to roll again: yes or no") #You've already printed the first number. Now, the user's input will be assigned to the variable yes. This line also executes every time you run the program
while (yes != "no"): #If the value assigned to yes is anything other than "no", control enters the loop. Once in the loop the only way out is if the value assigned to yes changes. Is there anything in this loop that could change the value assigned to yes?
number1 = random.randint(1,6) #assigns a new random integer to number1
if yes == ("yes"): #if the value assigned to yes is exactly "yes" the indented code for this 'if' block will execute, if the value is anything other than "yes", control goes directly back to the 'while' statement
print ("You got", number1) #prints
yes1 = input("Would you like to roll again: yes or no") #assigns user input to yes1, and control goes back to the 'while' statement
if yes == ("no"): #if the value of yes is exactly "no", the code inside the 'if' code block will execute
print ("Goodbye")
When you run your code, the first randomly generated number is going to be printed. Then you get user input. Unless the input is exactly “no” you enter the while loop. If the input is “no”, you skip down to the last if statement, and print “Goodbye”. Once control falls into the while loop, you have no way out, so the loop will repeat forever.