Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ending the Program
#1
So right now I am trying to figure out how to prompt the user to type in yes or no in order to either continue or end the game. I have the prompt but the program will not end only continue, please help.
Here is my code:
[python]
import random
import time
def main():
print("This is a rock, paper, scissor game")

while play_again.upper == 'Y':
return True
return False
play_again = True


def rps():
print("This is a rock, paper, scissor game")
time.sleep(3)
move_list = ['Rock', 'Paper', 'Scissors']
while play_again == True:
for comp in move_list:
user = input('What\'s your hand?: ')
user = user.capitalize()
comp = random.choice(list(move_list))
if comp == 'Rock' and user == 'Paper':
print("Computer throws " + comp + " you have thrown " + user + "!")
print("You Win!")

elif comp == 'Rock' and user == 'Rock':
print("Computer throws " + comp + " you have thrown " + user + "!")
print("It's a draw")

elif comp == 'Rock' and user == 'Scissor':
print("Computer throws " + comp + " you have thrown " + user + "!")
print("You Lose!")

elif comp == 'Scissors' and user == 'Paper':
print("Computer throws " + comp + " you have thrown " + user + "!")
print("You Lose!")

elif comp == 'Scissors' and user == 'Rock':
print("Computer throws " + comp + " you have thrown " + user + "!")
print("You Lose!")

elif comp == 'Scissors' and user == 'Scissors':
print("Computer throws " + comp + " you have thrown " + user + "!")
print("It's a Draw!")

elif comp == 'Paper' and user == 'Paper':
print("Computer throws " + comp + " you have thrown " + user + "!")
print("It's a Draw!")

elif comp == 'Paper' and user == 'Rock':
print("Computer throws " + comp + " you have thrown " + user + "!")
print("You Lose!")

elif comp == 'Paper' and user == 'Scissors':
print("Computer throws " + comp + " you have thrown " + user + "!")
print("You Win!")
else:
print("You messed up the program dummy")
print(comp)


answer = input("Would you like to play again?: ")

rps()
main()
Reply
#2
You can use a messagebox to promto for a Yes or No response if you are going to design this to run in a GUI.
Example:
import tkinter as tk
from tkinter import Label
from tkinter import Button
from tkinter import messagebox

root = tk.Tk()
root.geometry('300x300')

label=Label(root, text= 'Your gamre application is running')
label.pack()

def popup():
    your_popup = messagebox.askyesno('Your Game Name','Do you want to continue?')
    if your_popup == True:
        return
    else:
        root.destroy()
button=Button(root, text='Press', command=popup)
button.pack()

root.mainloop()
"Often stumped... But never defeated."
Reply
#3
call sys.exit()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  While loop not ending (Best of 10 dice game) K3nidi 3 1,456 Jul-09-2022, 09:53 AM
Last Post: K3nidi
  Python: Call function with variabele? Ending in error. efclem 5 2,879 Apr-22-2020, 02:35 PM
Last Post: buran
  ending process hrparra 1 1,881 Jan-07-2020, 07:40 PM
Last Post: ibreeden
  Ending loop with string (capital & lowercase) MattWilk97 3 3,411 Oct-22-2017, 09:13 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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