如何添加全局变量而不是本地

时间:2021-08-01 17:42:30

标签: python

以下是我的猜谜游戏代码。我在向全局变量中添加和制作一些局部变量时遇到了麻烦。某些变量,例如 gameStillGoing 或任何需要在整个游戏中使用的变量都需要是全局的。我尝试了一些方法,但似乎没有任何变化,并且出现了许多错误。我没主意了。请帮忙。

import random
 
def menu():
    print("Welcome")
    check=True
    while check:
        print("1-START \n2-EXIT")
        print("Please key in with 1 or 2 only")
        category=input("Choose your option:\n>")
        if category=='1':
            gameStart() #start the game
            check=False
        elif category=='2':
            print('Thank you and see you next time!')
            print("Exiting.")
            for i in range (1):
                quit()
        else:
            print('--Invalid choice!--\n')
            check= True
 
def gameStart ():
    #number generated by computer
    a = random.randint(1, 10)
 
    #this determines whether the game is going or end
    gameStillGoing = True
 
    #limit user to 5 guess
    counter = 5 
 
    num_inserted = [] 
 
    #Set loop condition
    while gameStillGoing:
 
    #print current value
        print ('Remaining = ', counter)
        print("You have " + str(counter)  +" chances to guess the number!\n")
 
        # Decrease guess counter by one each time to limit to 5 guesses
        counter = counter - 1
            
        #number input by user
        while True:
            num =input("Please enter an integer between 1-10: " )
            try:
                num = int(num)
                if num > 10 or num < 1:
                    print('Input out of range!') 
                    continue
                break
            except ValueError:
                print("This is not an integer")
        
        num_inserted.append(num)
 
        print (num_inserted)  
        
 
    #print number inserted and convert number to string to be printed out
        print("Number inserted:" + str(num))
    
        #if answer is wrong
        if (num != a):
            print("answer is wrong")
            # num = int(input("Please enter an integer between 1-10: " ))
 
        #if answer is right
        elif (num == a):
            print("answer is right")
 
        # End game after correct guess.
            gameStillGoing = False
 
        if (counter == 0):
           print("You ran out of tries! The num is: " , a ,"\n  luck next time.")
                                    
           # End game after correct guess.
           gameStillGoing = False
 
    
    #to indicated that the loop has been broken and the game has ended
    print("game has ended")

 
def restart ():
        menu()
        # print("Do you want to replay this game?")
        # print("Y \n:N")
 
        user_repeat = input("Do you want to play this game Y/N? ")

        while user_repeat != "N" and user_repeat != "Y" :
            print ("invalid")
            user_repeat = input("do you want to repeat Y/N ? ")

        return user_repeat

while True:

    user_repeat = restart()
 
    if user_repeat == "N":
                
        print ("thank you")
 
        break

0 个答案:

没有答案