Search code examples
python-3.xdictionaryuser-input

How do I print a new line in the program for the user to type in their answer on?


I am coding a Python dice game. I want the game to start when the user lets it.

So far I have this code:


number = random.randint(1, 6)

print("Do you want to roll the die?")
answer = input("Please type 'yes' or 'no': ")

dic = {"yes"}

while(True): 
    answer = input()
    if answer in dic:
        print("You have rolled: ", (number))
        break
    else:
        print("oh.")

But when the program runs, the user has to type "yes" in twice, for it to register it.

How do I skip the user having to type on the "Please type 'yes' or 'no': " line, so that they answer only once, on the line after it?

I have tried using print(), but I'm not sure where to put it so that it doesn't cause errors.

Thank you


Solution

  • The user is prompted twice because you're calling the input function twice. You only need to call it inside the loop.

    number = random.randint(1, 6)
    print("Do you want to roll the die?")
    dic = {"yes"}
    while(True): 
        answer = input("Please type 'yes' or 'no': ")
        if answer in dic:
            print("You have rolled: ", (number))
            break
        else:
            print("oh.")