我在为机会的基本游戏返回python 3中的函数时遇到问题

时间:2019-10-29 14:43:02

标签: python-3.x function return

这里是我认为引起问题的部分。

def update():
       player1_money=player1()
       player2_money=player2()
       player2_money=player2_money
       player1_money=player1_money
       return player1_money
       return player2_money
    update()       

OR

else:
                print("                         Sorry! Better luck next time.")
                player1_money=player1_money-bet1
                print (player1_name + "'s balance is now " , player1_money)
                break
            return (player1_money)
            return player1

这是我的代码供参考

from random import *
import time

print("This a game of chance. The computer chooses a random numeber from 1 to 30. ")
print("")
print("""You can bet on any number from 0 to 30.You will be given £100 to start with\n
and you can bet any  amount you want.\n
Here are the following condition for you to win.\n
If the random number you choose is also choosen by the computer and the number is:\n
Option 1.An even number you get 2x  money back.\n 
Option 2.A multiple of 10 you get 3x  money back.\n 
Option 3.A prime number you get 5x  money back.\n 
Option 4.Is below 5 you get a 2x bonus.""")

print("")
player1_name=input("                          Enter player one's name: ")
player2_name=input("                          Enter player two's name: ")



def player1():

   player1_money=100
   print(player1_name +"'s turn: ")
   print("")
   print(player1_name +"'s balance is now", player1_money)                   
   comp=randint(1,30)




   while True:

       try:                
            bet1=int(input("Enter the amount you want to bet: "))        
            if bet1>100 or bet1<1:
               print("                                            Invalid bet")
               continue

            user1=int(input("Please enter a number between 1 to 30: "))
            if user1>30 or user1<1:
               print("                       Invalid number")
               continue


            if user1%2 == 0 and comp%2==0 and comp==user1:
                print("You get 2x  money back.")
                player1_money = (bet1*2) + player1_money
                print("                      CONGRATS! Your lucky number matches option 1")
                print ("Player one's balance is now "  , player1_money)

                break

            if  user1%10==0 and comp%10==0 and comp==user1:
                print("You get 3x  money back.")
                player1_money = (bet1*3) + player1_money
                print("CONGRATS! Your lucky number matches option 2")
                print ("Player one's balance is now " , player1_money)

                break

            if user1>1 and user1<5:
                print("                      You get a 2x bonus.")
                player1_money = (bet1*2) + player1_money
                print("                      CONGRATS! Your lucky number matches option 3")
                print ("Player one's balance is now " , player1_money)
                break

            for i in range (2, user1):
                     if (user1%i)==0 and comp==user1:
                         print("                            You get 5x  money back.")
                         player1_money = (bet1*5) + player1_money
                         print("                   CONGRATS! Your lucky number matches option 4")
                         print ("Player one's balance is now " , player1_money)

                         break
            else:
                print("                         Sorry! Better luck next time.")
                player1_money=player1_money-bet1
                print (player1_name + "'s balance is now " , player1_money)
                break
            return (player1_money)
            return player1




       except:
           print("                        OOPS! Something went wrong.")
           cont1=input("Do you wish to restart?(Y or N): ").lower()
           while True:
               if cont1==("y"):
                  player1()
               elif cont1==("n"):
                    print("             Sorry!Closing game...")
                    time.sleep(2)
                    quit()
               else:
                  print("                 Invalid response!")



def update():
   player1_money=player1()
   player2_money=player2()
   player2_money=player2_money
   player1_money=player1_money
   return player1_money
   return player2_money
update()                






#--------------------------------------------------------------------------------------------------------------------------------------------



def player2():



   print("")
   print("==========================================================")
   print("")
   player2_money=100
   print(player2_name +"'s turn: ")
   print(player2_name +"'s balance is now", player2_money)



   comp=randint(1,30)

   while True:


       try:           



            bet2=int(input("Enter the amount you want to bet: "))        
            if bet2>100 or bet2<2:
               print("                       Invalid bet")
               continue

            user2=int(input("Please enter a number between 1 to 30: "))
            if user2>30 or user2<1:
               print("Invalid number")
               continue


            if user2%2 == 0 and comp%2==0 and comp==user2:
                print("You get 2x  money back.")
                player2_money = (bet2*2) + player2_money
                print("                      CONGRATS! Your lucky number matches option 1")
                print ("Player one's balance is now "  , player2_money)

                break

            if  user2%10==0 and comp%10==0 and comp==user2:
                print("You get 3x  money back.")
                player2_money = (bet2*3) + player2_money
                print("                   CONGRATS! Your lucky number matches option 2")
                print ("Player one's balance is now " , player2_money)

                break

            if user2>1 and user2<5:
                print("                      You get a 2x bonus.")
                player2_money = (bet2*2) + player2_money
                print("                      CONGRATS! Your lucky number matches option 3")
                print ("Player one's balance is now " , player2_money)

                break

            for i in range (2, user2):
                     if (user2%i)==0 and comp==user2:
                         print("You get 5x  money back.")
                         player2_money = (bet2*5) + player2_money
                         print("                   CONGRATS! Your lucky number matches option 4")
                         print ("Player one's balance is now " , player2_money)

                         break
            else:
                print("                Sorry! Better luck next time.")
                player2_money=player2_money-bet2
                print (player2_name + "'s balance is now " , player2_money)
                break
            return (player2_money)
            return player2





       except:
           print("OOPS! Something went wrong.")
           cont=input("Do you wish to restart?(Y or N): ").lower()
           while True:
               if cont==("y"):
                  player1()
                  break
               elif cont==("n"):
                  print("")
                  print("                          Sorry!Closing game...")
                  time.sleep(2)
                  quit()
               else:
                  print("                          Invalid response!")


def update():
   player1_money=player1()
   player2_money=player2()
   player2_money=player2_money
   player1_money=player1_money
   return player1_money
   return player2_money


player1()
player2()


while True:
   res=input("Do you want to continue?(Y/N): ").lower()
   if res==("y"):
      update()
      player1()
   elif res==("n"):
      print("                       Hope you enjoyed the game")
      time.sleep(2)
      quit()
   else:
      print("                       Invalid response!")

我的问题:

当我运行代码时,它运行得很好。 但是,当它运行一次后循环回到播放器1时,player1_money变量不会像我希望的那样更新。

我的问题:

  1. 如何更新代码,以便下次运行时更新“ player1_money”变量。

  2. 是否有其他方法可以编写此程序,从而减少所需的代码或以任何可能的方式有效?

0 个答案:

没有答案