簡體   English   中英

NameError:名稱“變量”未定義 Python

[英]NameError: name 'variable' is not defined Python

所以我不是 python 的專業人士,我在創建程序時遇到錯誤這里是代碼

# A game that will tell that the news is fake or not.

# Welcome message
print("Want to know that a news is fake or not?\nYou are at a right place. Welcome to FakeNewsDetector.org\nAt here we provide the news which is not fake and you can add more news if you want!!\nSo are you excited? Let's get started\n")

# User input + print for input
print("What thing you want do?")
print("1: Ask a news is fake or not?\n2: Tell a news it is fake or not\n")
number = int(input("We will answer your question just enter the number. Press ctrl+c anywhere if you want to exit: "))

# if then else



# Appending part
if number == 2:
    number = int(input("What is the number of news you are adding?: "))
    whatAreYouTelling = input("What news you want to say? Not include that the news is false or true: ")
    with open('news.txt', 'a') as f:
        a = f.write(f"\n{number}: {whatAreYouTelling}")
    
    trueOrFalse = input("Is the fact true(T) or false(F)?")
    if trueOrFalse == "t" or trueOrFalse == "T":
        fact = True

    elif trueOrFalse == "f" or trueOrFalse == "F":
        fact = False

    else:
        print("Invalid") 



# Reading part
elif number == 1:
    with open('news.txt', 'r') as f:
        a = f.read()
        print(a)

    news = int(input("What news do you want to ask? Enter number: "))

    if news == 1:
        fact = False
        print(f"The news is {fact}.")

    elif news == 2:
        fact = True
        print(f"The news is {fact}")

    elif news > 2:
        print(f"{trueOrFalse}")
else:
    print("Invalid")

# print("\nThanks for using our news service. I hope you like it\n Created by Anant Gupta\n Class 7D\n Homework: ATL")

當我運行我的代碼和 append 時,隨機值會出現這樣的錯誤

[anant@anant-hpprobook440g4 atlProject]$ python -u "/home/anant/Desktop/anant/codingPlayground/pythonProject/atlProject/main.py"
Want to know that a news is fake or not?
You are at a right place. Welcome to FakeNewsDetector.org
At here we provide the news which is not fake and you can add more news if you want!!
So are you excited? Let's get started

What thing you want do?
1: Ask a news is fake or not?
2: Tell a news it is fake or not

We will answer your question just enter the number. Press ctrl+c anywhere if you want to exit: 2
What is the number of news you are adding?: 3
What news you want to say? Not include that the news is false or true: Hello I am nothing
Is the fact true(T) or false(F)?F
[anant@anant-hpprobook440g4 atlProject]$ python -u "/home/anant/Desktop/anant/codingPlayground/pythonProject/atlProject/main.py"
Want to know that a news is fake or not?
You are at a right place. Welcome to FakeNewsDetector.org
At here we provide the news which is not fake and you can add more news if you want!!
So are you excited? Let's get started

What thing you want do?
1: Ask a news is fake or not?
2: Tell a news it is fake or not

We will answer your question just enter the number. Press ctrl+c anywhere if you want to exit: 1
1: Obama tweeted 'ashamed to shake hands with Modi'
2: Kamala Harris hasn't attacked Indian government on farmers
3: Hello I am nothing
What news do you want to ask? Enter number: 3
Traceback (most recent call last):
  File "/home/anant/Desktop/anant/codingPlayground/pythonProject/atlProject/main.py", line 51, in <module>
    print(f"{trueOrFalse}")
NameError: name 'trueOrFalse' is not defined
[anant@anant-hpprobook440g4 atlProject]$ 

我不是一個好的程序員,但請接受我的代碼。 我工作了 1 小時,但無法解決錯誤。 如果有人解決我將非常感謝。 提前致謝

TrueOrFalse的 scope 僅限於您的第一個if塊。 要使其在第二個塊中可訪問,您需要在if語句之外聲明它,例如首先將其設置為False

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM