-1

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()
ADTC
  • 8,999
  • 5
  • 68
  • 93
user2633836
  • 131
  • 3
  • 5
  • 13

1 Answers1

-1

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.

ADTC
  • 8,999
  • 5
  • 68
  • 93
  • No, the whitespace after while is fine. Python would probably throw an exception with this indentation style, it's unlikely that this is causing his issue. – Woodrow Douglass Jan 07 '14 at 13:35
  • Well I'm unfamiliar with Python. I just Googled up the tutorial. Better answers are always welcome! `:)` – ADTC Jan 07 '14 at 13:40