Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need help with my code
#1
1 import random
2
3
4 dice1 = random.randint(1,6)
5 dice2 = random.randint(1,6)
6 dice3 = random.randint(1,6)
7 dice4 = random.randint(1,6)
8
9
10 choise = input ("Welcome to my game! Wana play? yes/no")
11 if choise.lower() == "yes":
12 print ("-----------------------------------------")
13 elif choise != "yes":
14 print ("-----------------------------------------")
15 print ("Okay, goodbye.")
16 exit()
17
18
19 if dice1 == dice2:
20 print ("Unlucky, you scored 0.")
21 elif dice1 != dice2:
22 print ("You rolled a",dice1,"and a",dice2,"that makes",dice1 + dice2,)
23 choise2 = input ("Wana have one more go? yes/no")
24
25
26 if choise2.lower() == "yes":
27 print ("You rolled a",dice3,"and a",dice4,"that makes",dice3 + dice4,)
28 print ("All together that makes",dice1 + dice2 + dice3 +dice4,)
29 elif choise2 != "yes":
30 print ("Okay, goodbye.")



On line 16 I want to be able to end the program if they say no (so it doesn't carry on) without actually having to do exit(). What is the proper way I should do it?
Reply
#2
please, edit/repost your code using python tags, preserved indention and without line numbers
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Can you explain what you mean by end the program without having to exit()?

If you don't want the window to close on its own but the program to end, then you can just leave the line after print(Okay, Goodbye!) blank. Simple delete the exit() Wink
Since the program has no more lines to execute, it will sleep. So you can't interact anymore and the window is still open.

Presumably you want it like this so the player can see the exit message.
Reply


Forum Jump:

User Panel Messages

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