mardi 14 août 2018

Python: add counter on a game

I'm new to python and to programming in general, and I find loops to be very confusing. I don't often need to use loops, but I'm currently working on a simple game (Foot, Nuke, Cockroach, similar to Rock, Paper, Scissors) and I'm pretty sure I succeeded in the basic logic, however I can't get the logic of how to make a counter for the rounds, since I have to tie it to the if statement (I guess), or something like that. I also want to make it possible to count in how many rounds the player won, and how many times it was a tie. The player is playing against a computer which generates its reply based on randoms.

import random

number = random.randint(1,3)

if number == 1:
    chosen1 = "Foot"
elif number == 2:
    chosen1 = "Nuke"
else:
    chosen1 = "Cockroach"

chosen2 = input("Foot, Nuke or Cockroach? (Quit ends):")

def choice(chosen1, chosen2):
    if (chosen1 == "Nuke" and chosen2=="Nuke"):
        print("You chose: ", chosen2)
        print("Computer chose: ", chosen1)
        print("You LOSE!")
    elif chosen1 == chosen2:
        print("You chose: ", chosen2)
        print("Computer chose: ", chosen1)
        print("It is a tie!")
    elif (chosen1 == "Foot" and chosen2=="Cockroach"):
        print("You chose: ", chosen2)
        print("Computer chose: ", chosen1)
        print("You LOSE!")
    elif (chosen2 == "Foot" and chosen1=="Cockroach"):
        print("You chose: ", chosen2)
        print("Computer chose: ", chosen1)
        print("You WIN!")
    elif (chosen1 == "Nuke" and chosen2=="Foot"):
        print("You chose: ", chosen2)
        print("Computer chose: ", chosen1)
        print("You LOSE!")
    elif (chosen2 == "Nuke" and chosen1=="Foot"):
        print("You chose: ", chosen2)
        print("Computer chose: ", chosen1)
        print("You WIN!")
    elif (chosen1 == "Cockroach" and chosen2=="Nuke"):
        print("You chose: ", chosen2)
        print("Computer chose: ", chosen1)
        print("You LOSE!")
    elif (chosen2 == "Cockroach" and chosen1=="Nuke"):
        print("You chose: ", chosen2)
        print("Computer chose: ", chosen1)
        print("You WIN!")
    elif chosen2 =="Quit":
        quit()

while chosen2 != "Quit":
    choice(chosen1, chosen2)
    chosen2 = input("Foot, Nuke or Cockroach? (Quit ends):")

Can someone please advice me how to handle this loop?

Aucun commentaire:

Enregistrer un commentaire