Converting input to lower case to accept in a while loop - python

I am trying to create a program that takes an input and will convert whatever input for example Yes / YES to lower case to be accepted in the while loop below. Ive tried to do it but doesnt work. Any idea?
#Import the random function - This only needs to be imported once.
import random
#Use a while loop to allow the user to repeat the process
repeat = "YES"
while repeat == "yes":
#User is able to input which sided dice they want to throw.
dice = input("What side dice do you want to use? 4, 6, or 12?\n")
#4 sided
if dice == "4":
#Outputs what sided dice has been chosen and the score thay they rolled.
print(dice, "sided dice chose.\nYou rolled a", random.randint(1,4))
#6 sided
elif dice == "6":
print(dice, "sided dice chose.\nYou rolled a", (random.randint(1,6)))
#12 sided
elif dice == "12":
print(dice, "sided dice chose.\nYou rolled a", (random.randint(1,12)))
#Incorrect value entered
else:
#Informs the user that the number they have chosen is not a valid option.
print(dice, "is not a valid choice")
#Asks user if they want to use the program again.
print("Do you want to use the program again? Yes or No?")
#Links back to the start of the while loop.
repeat = input()

Your code will never even enter the while loop, as you set repeat to "YES" and then immediately check if it is "yes", which it isn't.

I would have solved this in another way
while True:
dice = input(....)
#etc
repeat = ''
while repeat.lower() not in ['yes', 'no']:
repeat = input('Do you want to use the program again? (yes/no)?')
if repeat.lower() == 'no':
break
This would ask the user to input yes or no, and keep on asking until yes or no in supplied.

Daniel is entirely correct. The 'YES' at the beginning of the code should be formatted as 'yes' because clearly 'YES does not equal 'yes' and thus the while loop won't run at all

Related

how to write dice rolling game project. this is my first project

did i write correct code
no1 = 1
no2 = 6
enter code here
dice = input("enter number: "):
no1 = int(input("enter number one: "))
no2 = int(input("enter number two: "))
print("you have enter "+str(no1))
print("you have enter "+str(no2))
if dice == "5":
print("you want to roll again")
elif dice == "6":
print("roll dice automatically")
check that you have not incorrectly formatted all of your question as code
There are many ways to write a dice rolling program. However, the simplest way would use a built-in library called random. Below I have fixed and cleaned the code:
import random # The libary used from generating random numbers
def dice_roll(): # function usedto reset the game
input("Press enter to roll the dice ")
print("The dice rolled: ", random.randint(0,6))
UserInputNew = input ("Would you like to roll again? ")
if UserInputNew == "5":
print("\n") # displays a blank line
dice_roll()
else:
print ("Thanks for playing!")
dice_roll() # loads the game
Above would do what you want. From your example above, you have loads of inputs which doesn't make sense from something like a dice roll. If you need help understanding certain bits of the code then I'll be more than happy to help.
help on this one
Alice, Bob and Carol have agreed to pool their Halloween candy and split it evenly among themselves. For the sake of their friendship, any candies left over will be smashed. For example, if they collectively bring home 91 candies, they'll take 30 each and smash 1.
Write an arithmetic expression below to calculate how many candies they must smash for a given haul.

Validating User Input in Python [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 3 years ago.
"""
GuessNumber.py - This program allows a user to guess a number
between 1 and 10.
Input: User guesses numbers until they get it right.
Output: Tells users if they are right or wrong.
"""
import random
number = random.randint(1, 10)
# Prime the loop.
keepGoing = input("Do you want to guess a number? Enter Y or N ")
# Validate input.
# Enter loop if they want to play.
while keepGoing == "Y":
# Get user's guess.
stringNumber = input("I'm thinking of a number. .\n Try to guess by entering a number between 1 and 10 ")
userNumber = int(stringNumber)
# Validate input.
# Test to see if the user guessed correctly.
if userNumber == number:
keepGoing = "N"
print("You are a genius. That's correct!")
else:
keepGoing = input("That's not correct. Do you want to guess again? Enter Y or N ")
# Validate input.
If the user guesses correctly, the program congratulates the user, and then the loop that controls guessing numbers exits; otherwise the program asks the user if he or she wants to guess again. If the user enters "Y", he or she can guess again. If the user enters "N", the loop exits. You can see that the "Y" or "N" is the sentinel value that controls the loop. I need to ass a code that validates "y" and "n"
keepGoing.upper() == "Y" would check lower and upper case inputs.
You shouldn't need to validate N/n
import random
number=random.randint(1,10)
keepGoing=raw_input("DO you want to guess a number:\n ")
keepGoing=keepGoing[0].upper()
while keepGoing=='Y':
stringNumber=int(raw_input("I'm thinking of a number. .\n Try to guess by entering a number between 1 and 10:\n "))
if (stringNumber==number):
print("You are a genius. That's correct!")
break
else:
keepGoing=raw_input("DO you want to guess a number")
keepGoing=keepGoing[0].upper()

How do I have python run (if statements) based on the users input.?

I am not doing anything particularly complicated I am simply messing with import random and having the user type roll to roll a six sided die. I have gotten this far.
import random
roll = random.randint(1,6)
input("Type roll to roll the dice!\n")
# This is where I have my issue pass this line I'm trying things out, unsuccessfully.
if (userInput) == (roll)
print("\n" + str(roll))
else:
input("\nPress enter to exit.")
I don't want the program to print the str(roll) if the use presses enter, I'd rather it exit the program if no input is given. So how do I write the code to do particular thing based of user input when using the if statement. If user input is 'roll" then print("str(roll))?
You need to capture the user input in a variable. Currently, the return value of input(…) is being thrown away. Instead, store it in userInput:
userInput = input("Type roll to roll the dice!\n")
The if requires a colon at the end in order to start the block:
if someCondition:
# ^
If you want to compare the user input against the string 'roll', then you need to specify that as a string, and not as a (non-existent) variable:
if userInput == 'roll':
You also don’t need parentheses around the values
In order to check for just an enter press, check against the empty string:
elif userInput == '':
print('User pressed enter without entering stuff')
You should roll inside the condition, not before, so you don’t generate a random number although it’s not requested.
So in total, it could look like this:
import random
userInput = input('Type roll to roll the dice!\n')
if userInput == 'roll':
roll = random.randint(1,6)
print('You rolled: ', roll)
elif userInput == '':
print('Exit')

Loop never seems to actually loop

print("Welcome to my dice game.")
print("First enter how many sides you would like your dice to have, 4, 6 or 12")
print("Then this program will randomly roll the dice and show a number")
#Introduction explaing what the game will do. Test 1 to see if it worked.
while True:
#starts a while loop so the user can roll the dice as many times as they find necessary
import random
#Imports the random function so the code will be able to randomly select a number
dice = int(input("Enter which dice you would to use,4, 6, or 12? "))
#set a variable for the amount of dice number
if dice == 12:
x = random.randint(1,12)
print("You picked a 12 sided dice. You rolled a " + str(x) + " well done")
#Test 2 see if it does pick a random number for a 12 sided di
elif dice == 6:
x = random.randint(1,6)
print("You picked a 6 sided dice. You rolled a " + str(x) + " well done")
#Test 3 see if it does pick a random number for a 6 sided di
elif dice == 4:
x = random.randint(1,4)
print("You picked a 4 sided dice. You rolled a " + str(x) + " well done")
#Test 4 see if it does pick a random number for a 4 sided di
else:
print("Sorry, pick either 12, 6 or 4")
#Test 5 tells the user that they can only pick 4, 6 or 12 if anything else is entered this error shows
rollAgain = input ("Roll Again? ")
if rollAgain == "no":
rollAgain = False
if rollAgain == "yes":
rollAgain = True
break
print ("Thank you for playing")
#if the user enters anything apart from yes y or Yes. The code ends here.
That is the code i have so far. However the code will never actually go to the beginning of the loop, no matter what i enter the code just displays "Thanks for playing" and ends. Can anyone please tell me where i have went wrong?
First, you should be using raw_input to get the user's selection. (assuming python 2) If you're using python 3 then input is fine and keep reading.
Anyway, it'll still quit when you type yes because you break out of the loop! You should move the break statement into the "no" case so it breaks out when you say you do not want to roll again.
rollAgain = raw_input ("Roll Again? ")
if rollAgain == "no":
break
You don't need to set rollAgain to true or false at all. With the above code, anything other than "no" is assumed to be "yes" but you can add checks for that easily.
The problem is that you break your loop when the user wants to roll the dice again. The loop should break when the player doesn't want to play again so you have to do :
http://pastebin.com/hzC1UwDM

How do you convert inputted data to uppercase so that it is recognised by a while loop?

I am trying to create a program that allows the user to keep using the program as long as what they enter is YES or a form of yes. I have created the program and it works and continues to loop providing they enter YES in uppercase.
I have tried to modify my code so that at the end when it asks if they would like to repeat it takes the input and converts it to uppercase I know you can use while repeat is in [" "," "] but is there a better way to write the code so that it will convert the inputted data to uppercase on the final input?
I have tried
repeat = input()
repeat = repeat.upper()
but this does not work. Any suggestions?
#Import the random function - This only needs to be imported once.
import random
#Use a while loop to allow the user to repeat the process
repeat = "YES"
while repeat == "YES":
#User is able to input which sided dice they want to throw.
dice = input("What side dice do you want to use? 4, 6, or 12?\n")
#4 sided
if dice == "4":
#Outputs what sided dice has been chosen and the score thay they rolled.
print(dice, "sided dice chose.\nYou rolled a", random.randint(1,4))
#6 sided
elif dice == "6":
print(dice, "sided dice chose.\nYou rolled a", (random.randint(1,6)))
#12 sided
elif dice == "12":
print(dice, "sided dice chose.\nYou rolled a", (random.randint(1,12)))
#Incorrect value entered
else:
#Informs the user that the number they have chosen is not a valid option.
print(dice, "is not a valid choice")
#Asks user if they want to use the program again.
print("Do you want to use the program again? Yes or No?")
#Links back to the start of the while loop.
repeat = input()
repeat = repeat.upper()
Your issue appears to be indentation. Since Python identifies code blocks using indentation, it's very important that you get this right.
I noticed your lines are indented as follows:
print("Do you want to use the program again? Yes or No?")
#Links back to the start of the while loop.
repeat = input()
repeat = repeat.upper()
Please try indenting them as follows, and it should work:
print("Do you want to use the program again? Yes or No?")
#Links back to the start of the while loop.
repeat = input()
repeat = repeat.upper()
Please look at the Lines and Indentation section here.
You may also have to remove the empty line after your while condition. But maybe not.
Hint: You won't have such problems when using whitespace-ignoring languages such as Java.

Categories

Resources