Python Forum
How to start the program from the beginning.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to start the program from the beginning.
#1
Hey guys,

im currently just trying to make very basic rock, paper, scissors game but i strumbled upon the following problem:

i wrote basic code to play Rock, Paper, Scissors. After every round i call the playagain() function to ask the user, if he wants to play again. If he does, i want the code to start over again.

Heres my Code:

import random



# user input
# random cpu for R,S,P
# compare results
# print winner
# Ask to play again

def playagain():
    print("do you want to play again?")
    choose = input("Yes or No?")
    choose = choose.upper()
    if choose == "YES":
        print("a") # THE PROBLEM IS HERE: IF THE USER INPUT EQUALS "YES", THEN START FROM THE BEGINNING
    else:
        exit()


# cpu options to randomly choose
options = ["R", "P", "S"]
cpu = [i for i in random.choice(options)]
cpu = cpu[0]

user = input("Choose [R]ock, [P]aper or [S]cissors: ").upper()

# What if user gives wrong input?
while user not in options:
    user = input("Choose [R]ock, [P]aper or [S]cissors: ").upper()

if cpu == user:
    print("Draw!")
elif user == "R" and cpu == "S":
    print("You Win!")
    playagain()
elif user == "R" and cpu == "P":
    print("Fucking looser. You Suck!")
    playagain()
elif user == "P" and cpu == "R":
    print("You Win!")
    playagain()
elif user == "P" and cpu == "S":
    print("you fucking suck!")
    playagain()
elif user == "S" and cpu == "R":
    print("you suck!")
    playagain()
elif user == "S" and cpu == "P":
    print("You Win!")
    playagain()
thanks ahead! Heart
Reply
#2
i did it! Big Grin

I used recursive functions and it worked
Reply
#3
(Feb-22-2021, 06:52 PM)iamaghost Wrote: i did it! Big Grin

I used recursive functions and it worked

That's not recursive. Recursion is if the function calls itself.
Reply
#4
You can look at his post for a way to do the repeat game task.
iamaghost likes this post
Reply
#5
i have a solution for you. even i wanted python to make a command like that but more better version: using a command you can go to any part of a program's code.
anyways, i have he solution.
my edited code:
import random
 
 
 
# user input
# random cpu for R,S,P
# compare results
# print winner
# Ask to play again
 
def playagain():
    print("do you want to play again?")
    choose = input("Yes or No?")
    choose = choose.upper()
    if choose == "YES":
        pass
    else:
        exit()
 
 
# cpu options to randomly choose
i = True
while i:

    options = ["R", "P", "S"]
    cpu = [i for i in random.choice(options)]
    cpu = cpu[0]
     
    user = input("Choose [R]ock, [P]aper or [S]cissors: ").upper()
     
    # What if user gives wrong input?
    while user not in options:
        user = input("Choose [R]ock, [P]aper or [S]cissors: ").upper()
     
    if cpu == user:
        print("Draw!")
    elif user == "R" and cpu == "S":
        print("You Win!")
        playagain()
    elif user == "R" and cpu == "P":
        print("Fucking looser. You Suck!")
        playagain()
    elif user == "P" and cpu == "R":
        print("You Win!")
        playagain()
    elif user == "P" and cpu == "S":
        print("you fucking suck!")
        playagain()
    elif user == "S" and cpu == "R":
        print("you suck!")
        playagain()
    elif user == "S" and cpu == "P":
        print("You Win!")
        playagain()
all i did was add a variable called i, starting with a bollean value of true
then instead of the print in your playagain() function, i put in pass, which basically is like no code, but if you dont type any code into the if command, then python freaks out. main thing i added a while loop .
while i: = basically means while True:
it will exit if no.
iamaghost likes this post
Reply
#6
Your code is upside down. Instead of a function that asks if you want to play again you should have a function that plays the game.
import random

choices = "RPS"
win = {"R":"S", "P":"R", "S":"P"}
throws = {"R":"Rock", "P":"Paper", "S":"Scissor"}

def get_input(prompt, choices):
    """Get user input.  Repeat until valid input is entered."""
    while True:
        choice = input(prompt).upper()
        if choice in choices:
            return choice

def play_game():
    """Play Rock Paper Scissor against computer until there is a winner."""
    while True: 
        computer = random.choice(choices)
        player = get_input("\nChoose [R]ock, [P]aper or [S]cissor: ", choices)

        if player == computer:
            print(f"Computer throws {throws[computer]}. Draw!")
        elif win[player] == computer:
            print(f"Computer throws {throws[computer]}. You Win!")
            break
        else:
            print(f"Computer throws {throws[computer]}. You lose.")
            break

# Play until player wants to quit
while True:
    play_game()
    if get_input("\nDo you want to play again? [Y,N]", "YN") != "Y":
        break
Since user input figures so importantly in the game I wrote a function to simplify getting valid input.
iamaghost likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Just beginning dalekeel 2 233 Apr-15-2024, 11:49 AM
Last Post: Pedroski55
  Program doesnt return beginning bilisim19 2 940 Feb-15-2023, 06:23 PM
Last Post: Larz60+
  List of dataframe values beginning with x,y or z glidecode 3 1,949 Nov-08-2021, 10:16 PM
Last Post: glidecode
  Start my program in RPI4B from SSH and Failed ATARI_LIVE 3 3,531 Nov-11-2020, 11:19 AM
Last Post: ATARI_LIVE
  Problem: Once I cancel the process my program will start working! Hadad 0 1,660 Jul-24-2019, 04:09 PM
Last Post: Hadad
  want to change the beginning of the result Rudinirudini 5 3,602 Nov-15-2018, 11:28 AM
Last Post: Rudinirudini
  How can I start a python program out of a python program? peer 3 2,322 Oct-30-2018, 05:07 PM
Last Post: Larz60+
  What's the difference b/w assigning start=None and start=" " Madara 1 2,325 Aug-06-2018, 08:23 AM
Last Post: buran
  Beginning of Beginner Help: Should I start with Python? appdevelnewb 2 3,060 Jul-23-2018, 11:17 PM
Last Post: appdevelnewb
  Wrap from end to beginning. 27 to 1, 28 to 2 etc DreamingInsanity 5 3,708 Jun-24-2018, 01:02 PM
Last Post: ljmetzger

Forum Jump:

User Panel Messages

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