Python Forum
Hi, my loop yes/no part doesn't work and I really need help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hi, my loop yes/no part doesn't work and I really need help
#1
Hey everyone, so I am in secondary school and have had to make a 'big program', and I have to include a yes/no in it to get the grade 8 (a*), whenever I type in no it just continues when it isn't supposed to, could you guys help me out please?

import random
import time
play = True
while play == True:
    print("Welcome to the roll the dice game!")

    player1 = input("What is your name, player1?")
    time.sleep(3)
    player2 = input("What is your name, player2?")

    print(player1,"and", player2 ," ", "Welcome to roll the dice!")

    promt = input(" Player1! You are chosen to roll the dice, press enter!")
    
    print("The dice are rolling!!! \n" * 4)
    time.sleep(3)
    choice = random.randint(1,6)
    if choice == 1:
            print(player1, " Your number is 1")
    elif choice == 2:
            print(player1, " Your number is 2")
    elif choice == 3:
            print(player1, " Your number is 3")
    elif choice == 4:
            print(player1, " Your number is 4")
    elif choice == 5:
            print(player1, " Your number is 5")
    elif choice == 6:
            print(player1, " Your number is 6")

    promt2 = input(" Player2 You are chosen to roll the dice, press enter!")

    print("The dice are rolling!!! \n" * 4)
    time.sleep(3)
    choice2 = random.randint(1,6)
    if choice2 == 1:
            print(player2, " Your number is 1")
    elif choice2 == 2:
            print(player2, " Your number is 2")
    elif choice2 == 3:
            print(player2, " Your number is 3")
    elif choice2 == 4:
            print(player2, " Your number is 4")
    elif choice2 == 5:
            print(player2, " Your number is 5")
    elif choice2 == 6:
            print(player2, " Your number is 6")

    if choice > choice2:
        print(player1, "Has won the game, bad luck" ," ", player2)
    if choice2 > choice:
        print(player2, "Has won the game, bad luck" ," ", player1)
        

    restart = input("Would you like to try again?")

    if restart == "yes" or "y":
        continue

    else:
        print("Good bye :)")
        play = False
        
     
This isn't homework by the way, I have just decided to do it so that I can get a better grade.
Reply
#2
https://python-forum.io/Thread-Multiple-...or-keyword
Reply
#3
The statement:
if restart == "yes" or "y":
is equivalent to:
if restart == "yes" or "y" == "y":
which will always return true

it should be:
if restart == "yes" or restart == "y":
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop does not work in python Ola92 11 4,786 Jan-13-2020, 06:27 PM
Last Post: Ola92
  Issues with while loop, while (var != 0): does not work, but while (var !=''): works danilo 2 2,047 Apr-15-2019, 12:08 AM
Last Post: danilo
  Why doesn't this print function work? wlsa 3 3,386 Jun-15-2018, 04:01 PM
Last Post: woooee

Forum Jump:

User Panel Messages

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