Python 蟒蛇区域训练员

Python 蟒蛇区域训练员,python,python-2.7,if-statement,boolean,Python,Python 2.7,If Statement,Boolean,用户从多项选择中输入一个答案,他们认为该答案是所选形状尺寸的正确区域,程序应该检查答案是否正确,然而,在它不断输出该区域不正确的那一刻,即使该区域是正确的 import random area = 0 choice = 0 correct = False username = input("please input a username ") password = input("please input a password ") incorrect = 0 options = [] while

用户从多项选择中输入一个答案,他们认为该答案是所选形状尺寸的正确区域,程序应该检查答案是否正确,然而,在它不断输出该区域不正确的那一刻,即使该区域是正确的

import random
area = 0
choice = 0
correct = False
username = input("please input a username ")
password = input("please input a password ")
incorrect = 0
options = []
while len(password) < 6:
    print("Your password has to be at least 6 digits long ")
    password = input("please input a password ")

while username == password:
    print("your password can't be the same as your username ")
    password = input("please input a password ")

repass = input("please re-enter your password ")

while repass != password:
    repass = input("please re-enter your password ")

def play(choice, area, options):    
    while choice == 0:
        try:
            choice = int(input("Pick a shape: 1. triangle, 2.rectangle, 3.circle "))
        except ValueError:
            print("you must enter a number ")
            continue

    while area == 0: #if they pick an inncorrect number one than once is will replay error until they pick a correct one
        if choice == 1:
            print("you chose to the area of a triangle ")
            base = random.randint(1,30)
            height = random.randint(1,30)
            area = base * height // 2
            working = ("base * height // 2")
            print("the base of the triangle is",base,"cm and the height is",height,"cm")

        elif choice == 2:
            print("you chose to calculate the area of a rectangle ")
            base = random.randint(1,30)
            height = random.randint(1,30)
            while base == height: #stops the rectangle from becoming a square
                height = random.randint(1,30)
            area = base * height
            working = ("base * height")
            print("the base of the rectangle is",base,"cm and the height is",height,"cm")

        elif choice == 3:
            print("you chose to calculate the area of a circle ")
            radius = random.randint(1,30)
            rsquared = radius * radius
            pi = 3.141592653
            area = pi * rsquared 
            working = ("pi * rsquared")
            print("the radius of your circle is",radius,"cm")

        else: #stop's programme from breaking
            print("you must pick a number out of 1,2 and 3 ")
            try:
                choice = int(input("Pick a shape: 1. triangle, 2.rectangle, 3.circle "))
            except ValueError:
            continue

options.append(area)
while len(options) < 4:
    randans = random.randint(1,900)
    options.append(randans)
random.shuffle(options) # puts the answers in random order in array so its never the same letter for the answer
print("1.",options[0],"2.",options[1],"3.",options[2],"4.",options[3])
return options, area

play(choice, area, options)
print(options)   
def guess_ans(options, area, incorrect):
    answer = int(input("input your answer "))
    if answer == 1 and options[0] == area:
        print("true")
    elif answer == 2 and options[1] == area:
        print("true")
    elif answer == 3 and options[2] == area:
        print("true")
    elif answer == 4 and options[3] == area:
        print("true")
    else:
        print("false")
        incorrect += 1
    return incorrect
guess_ans(options, area, incorrect)

if incorrect == 1:
    print("the correct formula for working out the answer is",working)
    guess_ans(options, area, incorrect)
elif incorrect < 1:
    playagain = str(input("would you like to play again? Y or N "))
    if playagain in ['y', 'Y', 'yes', 'Yes', 'YES']:
        play(choice, area, options)
        guess_ans(options, area, incorrect)
随机导入
面积=0
选择=0
正确=错误
用户名=输入(“请输入用户名”)
密码=输入(“请输入密码”)
不正确=0
选项=[]
而len(密码)<6:
打印(“您的密码长度必须至少为6位”)
密码=输入(“请输入密码”)
用户名==密码时:
打印(“您的密码不能与您的用户名相同”)
密码=输入(“请输入密码”)
repass=输入(“请重新输入密码”)
当重新通过时!=密码:
repass=输入(“请重新输入密码”)
def播放(选项、区域、选项):
当选项==0时:
尝试:
choice=int(输入(“拾取形状:1.三角形,2.矩形,3.圆形”))
除值错误外:
打印(“您必须输入一个数字”)
持续
而area==0:#如果他们选择了一个正确的数字,则会重复错误,直到他们选择了一个正确的数字
如果选项==1:
打印(“您选择了三角形的区域”)
base=random.randint(1,30)
高度=随机。随机数(1,30)
面积=基础*高度//2
工作=(“底座*高度//2”)
打印(“三角形的底面为”,底面为,“厘米”,高度为”,高度为,“厘米”)
elif选项==2:
打印(“您选择计算矩形的面积”)
base=random.randint(1,30)
高度=随机。随机数(1,30)
而base==height:#阻止矩形变成正方形
高度=随机。随机数(1,30)
面积=基础*高度
工作=(“基准*高度”)
打印(“矩形的底部为”,底部为“,厘米,高度为”,高度为“,厘米”)
elif选项==3:
打印(“您选择计算圆的面积”)
半径=随机。随机数(1,30)
rsquared=半径*半径
pi=3.141592653
面积=π*平方
工作=(“pi*rsquared”)
打印(“圆的半径为”,半径为,厘米”)
否则:#停止节目中断
打印(“您必须从1、2和3中选择一个数字”)
尝试:
choice=int(输入(“拾取形状:1.三角形,2.矩形,3.圆形”))
除值错误外:
持续
选项。追加(区域)
而len(选项)<4:
randans=random.randint(1900)
options.append(随机数)
随机。洗牌(选项)#将答案随机排列在数组中,因此答案的字母永远不会相同
打印(“1.”,选项[0],“2.”,选项[1],“3.”,选项[2],“4.”,选项[3])
返回选项,区域
播放(选项、区域、选项)
打印(选项)
def猜测(选项、区域、错误):
答案=int(输入(“输入您的答案”))
如果答案==1,选项[0]==区域:
打印(“真实”)
elif答案==2,选项[1]==区域:
打印(“真实”)
elif答案==3,选项[2]==区域:
打印(“真实”)
elif答案==4,选项[3]==区域:
打印(“真实”)
其他:
打印(“假”)
不正确+=1
返回错误
猜测(选项、区域、错误)
如果不正确==1:
打印(“计算答案的正确公式是”,工作)
猜测(选项、区域、错误)
elif不正确<1:
playreach=str(输入(“您想再次播放吗?Y还是N”))
如果在['y','y','yes','yes','yes']中再次播放:
播放(选项、区域、选项)
猜测(选项、区域、错误)

更改他们给出答案的输入

float(input(

你需要把你的例子减少到最低限度。看见