Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with Loop.
#1
Hello!

This is my first post so I apologize if Ive done this incorrectly.

I am a beginner and this is only my third day of learning python. I attempted to create my first mini game(without looking online for help) where it asks "What is your guess?" for what you think the dice will roll and it will reveal the answer, then print if you won or lose.

I created that pretty quickly but realized I wanted it to ask if the player wanted to play again. So I created a function and initially thought it worked but actually after the second time through the program ends.

My question is why does it end after the second play through and how can I fix this?





import random
import time


user_answer = input("Would you like to try and guess the number the dice will roll? : ")

def guess_number():

    dice_sides = ["1", "2", "3", "4", "5", "6"]



    r_roll = random.choice(dice_sides)

    if user_answer == "yes" or "Yes":
        print("Excellent, lets play!")

    else:
        print("Okay, have a great day!")

    user_guess = input("What is your guess? : ")



    time.sleep(1)

    print("The number is.....")

    time.sleep(1.5)

    print(r_roll)

    if user_guess == r_roll:
     print("Congrats! You won!")

    else:
     print("Sorry, you lose....")

guess_number()

again = input("Do you want to play again? : ")

if again == "yes" or "yes":
    guess_number()
else:
    print("Have a good day!")
Reply
#2
You need a loop. Are you familiar with any programming languages? If so, please forgive. A loop allows you to run the same code over and over without having to write the code over and over. You would have a loop that repeatedly called guess_number() over and over until some condition is met. For example
done = 'N'
While done != 'Y':
   guess_number()
   done = input('Would you like to play again (Y/N)? ')
Python has other kinds of loops that are useful for doing different kinds of processing.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020