'Why does my code make me type "yes" or "no" twice to get the result I want instead of just once?

Why does this code make me type yes or no twice to get the result I want instead of just once?

This is for the python dice roll text game, btw...

import random
min = 1
max = 20

# <!--TWO D-20's, A-LA DUNGEONS AND DRAGAONS--!>

roll_again = "yes"

while roll_again == "yes" or roll_again == "y":
    print("Rolling the dice")
    print("The values are --- ")
    print(random.randint(min, max))
    print(random.randint(min, max))
    roll_again = input("Would you like to play again?")
    answer = input()
if answer == ('yes'):print("OK, here we go!")
elif answer == ("no"):print("Sorry about that, please try again another time.")

I am entering into a python class on Monday, this is one of the more common types of beginners code (granted I spiced it up by changing the dice from 6 sided to 20 sided, but it's more fun that way...lol) that I have read in some of the python books and sites I have visited, so I wanted to kind of get my feet wet a bit before I start my class.

So, any idea why I have to type yes or no twice, hitting enter after each time, to get it to run properly?

For the record, I am on Win10 right now but I also mostly use Parrot Security OS (Linux)...

Thanks for any and all feedback which anyone can provide...I know it's probably a stupid noob mistake or oversight, since I don't really know or understand the basics, but the quicker I can grasp them the better...



Solution 1:[1]

Every time you call the input() function, it prompts for input. Since you call it twice for each iteration of your while loop, the user is prompted twice per iteration. You should instead only call the input() function once in your while loop.

You can also avoid using the answer variable if you just use the roll_again variable for your conditions for your if and elif.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shane Bishop