Ivo Sorokins
Ivo Sorokins

Reputation: 15

Doesn't store entered value when runs second time

I made a rock, paper, scissors game. It works fine, as I wanted to, but it doesn't store entered value when runs second time, how do I fix that.

See the while loop below where the program goes again if the user enters y.

import random

tools =["Rock","Scissors","Paper"]
r = "".join(tools[0])
s = "".join(tools[1])
p = "".join(tools[-1])

def computer_predicion():
    computer_pred = random.choice(tools)
    return computer_pred

computer = computer_predicion()

def my_predicion():
    my_predicion = input("Choose (R)Rock,(S)Scissors,(P)Paper:")
    if my_predicion == "R" or my_predicion == "r":
        my_predicion = r
        return my_predicion
    elif my_predicion == "S" or my_predicion == "s":
        my_predicion = s
        return my_predicion
    elif my_predicion == "P" or my_predicion == "p":
        my_predicion = p
        return my_predicion
    else:
        print("Debils ir?")

human=my_predicion()

def game():
    message_win = ("You won!")
    message_lose = ("You lost!")
    message = "Computer:%s\nUser:%s"%(computer, human)
    if computer == r and human == r :
        print(message+"\nIt's draw")
    elif computer == p and human == p:
        print(message + "\nIt's draw")
    elif computer == s and human == s:
        print(message + "\nIt's draw")
    elif computer == r and human == p:
        print(message+'\n'+message_win)
    elif computer == p and human == r:
        print(message+'\n'+message_lose)
    elif computer == r and human == s:
        print(message+'\n'+message_lose)
    elif computer == s and human == r:
        print(message+'\n'+message_win)
    elif computer == p and human == s:
        print(message+'\n'+message_win)
    elif computer == s and human == p:
        print(message+'\n'+message_lose)
    else:
        pass

c = True
while c:      //Here code runs second time if user inputs Y or y.
    game()
    h = input("Continue?(Y/N):")
    if h == "Y" or h == "y":
        my_predicion()
        computer_predicion()
        pass
    elif h == "N" or h == "n":
        c = False
    else:
        print("Wrong symbol!")

Upvotes: 1

Views: 31

Answers (1)

devios
devios

Reputation: 235

The value is not being stored in the second loop because

computer = computer_predicion()
human = my_predicion()

are displayed outside of the while loop

Here is your working code, I migrated the two variable assignment inside the while loop

import random

tools =["Rock","Scissors","Paper"]
r="".join(tools[0])
s="".join(tools[1])
p="".join(tools[-1])
def computer_predicion():
    computer_pred = random.choice(tools)
    return computer_pred

def my_predicion():
    my_predicion = input("Choose (R)Rock,(S)Scissors,(P)Paper:")
    if my_predicion=="R" or my_predicion =="r":
       my_predicion = r
       return my_predicion
    elif my_predicion=="S" or my_predicion =="s":
       my_predicion = s
       return my_predicion
    elif my_predicion=="P" or my_predicion =="p":
       my_predicion = p
       return my_predicion
    else:
         print("Debils ir?")

def game():
    message_win = ("You won!")
    message_lose = ("You lost!")
    message = "Computer:%s\nUser:%s"%(computer,human)
    if computer ==r and human==r :
       print(message+"\nIt's draw")
    elif computer == p and human == p:
       print(message + "\nIt's draw")
    elif computer == s and human == s:
       print(message + "\nIt's draw")
    elif computer == r and human==p:
       print(message+'\n'+message_win)
    elif computer == p and human==r:
       print(message+'\n'+message_lose)
    elif computer == r and human==s:
       print(message+'\n'+message_lose)
    elif computer == s and human==r:
       print(message+'\n'+message_win)
    elif computer == p and human == s:
       print(message+'\n'+message_win)
    elif computer == s and human==p:
       print(message+'\n'+message_lose)
    else:
        pass

c=True
while c :      #Here code runs second time if user inputs Y or y.
  computer = computer_predicion()
  human = my_predicion()
  game()
  h = input("Continue?(Y/N):")
  if h=="Y" or h=="y":
     pass
  elif h=="N" or h=="n":
      c=False
  else:
       print("Wrong symbol!")

Upvotes: 1

Related Questions