简体   繁体   中英

Python is saying that a variable has not been assigned when it has

import random
import time
name=input("Welcome to the game what is your name")
print(("This is a numbers game"),(name),("you will be playing against the computer."))
print("The idea of the game is to get closer to 21 to the computer without going over 21"),
ready="N"
ready=input("Are you ready to play the game yet?").lower
if ready=="yes" or ready=="y":
    score=0
    while (score)<21 and (ready == "Y" or ready == "Yes" or ready =="YES" or ready == "yes" or ready =="y"):
        player1=random.randint(1,21)
        score=(score+player1)
        time.sleep(3)
        print(("you have scored"),(score))
        if score <21:
            ready=input("Do you want to add more to your score?")
if score>21: *THE PROBLEMS ON THIS LINE HERE*
    print("Sorry Over 21 , The Computer Wins!")
else:
    print("ok Well done let us see what the computer can do with their turn")
    computerscore=0
    while(computerscore)<21 and (computerscore)<(score):
        computer=random.randint(1,21)
        computerscore=(computerscore+computer)
        time.sleep(3)
        print(("The computer has scored"),(computerscore))
        if (computerscore)<=21 and (computerscore)>(score):
            print("Sorry the computer wins")
        else:
            print("You win well done")
        break

I get an error that says if score>21: NameError: name 'score' is not defined

but I have put score=0 so isn't that defining it?

I am guessing you are inputting the value yes correctly, the issue may be because of the line -

ready=input("Are you ready to play the game yet?").lower

You are assigning the reference to the lower function in ready , instead you should call lower() and assign the return value in ready . Example -

ready=input("Are you ready to play the game yet?").lower()

Also, if you want your code to work, when you do not input ready as yes , you should set score=0 , before the if condition - if ready=="yes" or ready=="y":

The variable score is declared inside an if block.

Suppose ready is different from "yes" and "y" , then the line score=0 is never reached.

So if score>21: raises an error.

You have to set a default value like score = 0 before entering to the first if condition.

Moreover, you made a typo:

ready=input("Are you ready to play the game yet?").lower

You should call the function using .lower() .

You need to intend the if score>21: line and everything that follows so that part of the code is included within the upper if coverage.

import random
import time
name=input("Wecome to the game what is your name")
print(("This is a numbers game"),(name),("you will be playing against the computer."))
print("The idea of the game is to get closer to 21 to the computer without going over 21"),
ready="N"
ready=input("Are you ready to play the game yet?").lower
if ready=="yes" or ready=="y":
    score=0
    while (score)<21 and (ready == "Y" or ready == "Yes" or ready =="YES" or ready == "yes" or ready =="y"):
        player1=random.randint(1,21)
        score=(score+player1)
        time.sleep(3)
        print(("you have scored"),(score))
        if score <21:
            ready=input("Do you want to add more to your score?")
    if score>21: *THE PROBLEMS ON THIS LINE HERE*
        print("Sorry Over 21 , The Computer Wins!")
    else:
        print("ok Well done let us see what the computer can do with their turn")
        #same with the rest

On a different note, this script might be flawed because there is a lot of possibility that someone will key in something different. You may want to include in the line:

ready=input("Are you ready to play the game yet? Type 'Y' for Yes").lower()

some more text about what you want the player to specifically put in.

However, as mentioned above, the main point is lower() , this contributes to the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM