如何根据随机列表获得真值或假值



我现在有一个" for fun ";我正在进行的项目构想。从本质上讲,程序输出一个报价,用户必须说出它是真实的还是编造的(1和2作为输入)。我现在的代码是'

#the point of the code is to select a random quote from either t or f and use user input to see if they think it's real or fake
import random
def conserv():
    t="This is a test true quote","This is another true quote" #quote variables
    f="This is a test fake quote","This is also fake"
    fact=0
    while fact=0: #setting up a while loop
        u=int(input("is the following quote real or did I make it up? ")) #Variables will be defined in the welcome function
        l=random.randchoice(t,f) #select a either t or f at random
        if l==t and u==1: #if the true list is selected and they say it's true
            print("yes! that is a real quote!")
        if l==t and u==2: #if the true list is selected and they say it's false
            print("nope, that is real")
        if l==f and u==1: #if the false list is selected and they say it's true
            print("nope, I actually made it up")
        if l==f and u==2: #if the false is selected and they say it's false
            print("correct! I made it up")
        if u==0:
            return #end the program
def welcome():
    print("Welcome to fact or BS! I give you a quite and you type in 1 if it's true, 2 if it's false, or 0 to end the program!")
def main():
    welcome()
    conserv()
                

what am I doing wrong here?

I tried messing with indentation but I'm not sure what exactly to try from there

你的代码中有一些错误:-

行不。7是while fact==0

您正在检查这里的条件。所以你需要写两个等号

行不。9模块"随机"没有属性randchoice,只有choice。您需要将参数作为列表或其他迭代器。

最后你甚至没有调用main()函数。

#the point of the code is to select a random quote from either t or f and use user input to see if they think it's real or fake
import random
def conserv():
    t="This is a test true quote","This is another true quote" #quote variables
    f="This is a test fake quote","This is also fake"
    list = [t,f]
    fact=0
    while fact==0: #setting up a while loop
        u=int(input("is the following quote real or did I make it up? ")) #Variables will be defined in the welcome function
        l=random.choice(list) #select a either t or f at random
        if l==t and u==1: #if the true list is selected and they say it's true
            print("yes! that is a real quote!")
        if l==t and u==2: #if the true list is selected and they say it's false
            print("nope, that is real")
        if l==f and u==1: #if the false list is selected and they say it's true
            print("nope, I actually made it up")
        if l==f and u==2: #if the false is selected and they say it's false
            print("correct! I made it up")
        if u==0:
            return #end the program
def welcome():
    print("Welcome to fact or BS! I give you a quite and you type in 1 if it's true, 2 if it's false, or 0 to end the program!")
def main():
    welcome()
    conserv()
main()

最新更新