我的python代码有什么问题?我无法获得任何输出。?

2024-10-02 12:36:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法理解代码中的错误,为什么没有得到任何输出?
我已经彻底检查了我的代码,但仍然找不到任何错误。 当我试着在pycharm上运行它时,它什么也没做

import random
    def ques():
        name=input("What's your name?:")
        print("Hi",name,"!")
    
        choice=random.choice("+x")
        finish=False
        quesno=0
        correctques=0
    
        while finish == False:
            if quesno<10|questno>=0:
                no1=random.randrange(1,10)
                no2=random.randrange(1,10)
                print((no1),(choice),(no2))
                ans=int(input("What's the answer?:"))
                quesno=quesno+1
    
                if choice==("+"):
                    realans=no1+no2
                    if ans==realans:
                        print("That's the correct answer!")
                        correctques=correctques+1
                    else:
                        print("That's incorrect!,the answer was",realans,"!")
    
            if choice==("x"):
                realans=no1*no2
                if ans==realans:
                    print("That's the correct answer!")
                    correctques=correctques+1
                else:
                    print("That's incorrect!,the answer was",realans,"!")
    
            elif choice==("-"):
                realans=no1-no2
                if ans==realans:
                    print("That's the correct answer")
                    correctques=correctques+1
                else:
                     print("That's incorrect!,the answer was",realans,"!")
            else:
                finish=True
        else:
             print("Good Job",name,"! You have finished the quiz")
            

Tags: theanswernameifthatrandomelseprint
3条回答
import random


def ques():
    name = input("What's your name?:")
    print("Hi", name, "!")

    choice = random.choice("+x")
    finish = False
    quesno = 0
    correctques = 0

    while finish == False:
        if quesno < 10 | quesno >= 0:
            no1 = random.randrange(1, 10)
            no2 = random.randrange(1, 10)
            print((no1), (choice), (no2))
            ans = int(input("What's the answer?:"))
            quesno = quesno + 1

            if choice == ("+"):
                realans = no1 + no2
                if ans == realans:
                    print("That's the correct answer!")
                    correctques = correctques + 1
                else:
                    print("That's incorrect!,the answer was", realans, "!")
                    finish = True

        if choice == ("x"):
            realans = no1 * no2
            if ans == realans:
                print("That's the correct answer!")
                correctques = correctques + 1
            else:
                print("That's incorrect!,the answer was", realans, "!")
                finish = True

        elif choice == ("-"):
            realans = no1 - no2
            if ans == realans:
                print("That's the correct answer")
                correctques = correctques + 1
            else:
                print("That's incorrect!,the answer was", realans, "!")
                finish = True
        else:
            finish = True
    else:
        print("Good Job", name, "! You have finished the quiz")
ques()

您需要调用该函数。 在代码末尾,只需键入ques(),这将调用function,您应该能够获得输出

你跑了吗?将此添加到代码末尾

ques()
if __name__ == "__main__":
   ques()

相关问题 更多 >

    热门问题