NameError:未在Python中定义名称“variable”

2024-09-27 21:31:56 发布

您现在位置: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")

当我运行代码并附加一个随机值时,会出现如下错误

[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个小时,无法解决错误。如果有人能解决问题,我将非常感激。提前谢谢


Tags: orthetoyounumberifisnot
1条回答
网友
1楼 · 发布于 2024-09-27 21:31:56

TrueOrFalse的作用域仅限于第一个if块。要使它在第二个块内可访问,您需要在if语句之外声明它,例如将它设置为Falsefirst

相关问题 更多 >

    热门问题