Python区域训练师

时间:2017-03-25 21:50:22

标签: 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 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)

1 个答案:

答案 0 :(得分:0)

更改输入的答案

float(input(