在python程序中处理用户输入

2024-09-29 22:05:18 发布

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

我正在做一个程序,给用户一个从1到10的随机整数作为一个直径使用一个列表,他们必须计算一个圆的周长使用圆周率为3。在我的代码中,当用户输入字符串而不是整数时,程序会要求用户输入整数。如果用户输入了正确的答案,程序会说这是一个不正确的答案。我需要程序证明它是正确的。 有没有可能我能得到一些帮助?这是我的代码:

import turtle
import random
turtle.speed("fastest")


pi = 3
minNumber = 4
maxNumber = 10
score = 0
listNmbers = []
a = [1,3,5,7,9]

red = random.random()
green = random.random()
blue = random.random()

num1 = random.choice(a)


def askCir(cirAnswer):
    try: 
        user = input("What is the circumference of a circle if the diameter is " + str(num1) + " and Pi is 3?")
        cirAnswer = int(user)   
    except:
        print("Please input a number only!")
        cirAnswer = 0;
        cirAanswer = askCir(cirAnswer)
    return cirAnswer
    cirAnswer = 0;
    cirAnswer = askCir(cirAnswer)



print("Welcome! What is your name??")
name = str(input())
print("Hello", name,"you need to calculate the circumference of a circle when given a diameter. To calculate the circumference, use the equasion; Pi x Diameter (Pi = 3")


def getNumbers():
    num = input("how many questions would you like to answer? (Pick between 5 and 10)")
    try:
        numbers = int(num)
    except:
        print("That is not a number!")
        return getNumbers()
    goodInput = minNumber < numbers < maxNumber

    if not goodInput:
        print ("That is not between 5 and 10. Please input an integer between 5 and 10.")
        return getNumbers()
    else:
        return numbers
numbers = getNumbers()    


for i in range(numbers):
    red = random.random()
    green = random.random()
    blue = random.random()
    turtle.color(red,green,blue)
    num1 = random.choice(a)
    correct = num1 * 3

    cirAnswer = 0;
    cirAnswer = askCir(cirAnswer)
    print(str(correct))

    if float (cirAnswer) == correct:
        print("That's Correct! Well Done")
        score = score + 1
    else:
        print("Sorry that is incorrect")

    for k in range(correct):
        turtle.color(red,green,blue)
        drawSquare()

    turtle.penup()
    turtle.forward(150)

Tags: the用户程序inputisrandomgreenblue
1条回答
网友
1楼 · 发布于 2024-09-29 22:05:18

它不工作是因为你的程序中的一个错误。在

cirAanswer = askCir(cirAnswer)需要

cirAnswer = askCir(cirAnswer)

基本上,新的答案被分配给一个新的变量cirAanswer,并且总是返回0

相关问题 更多 >

    热门问题