如何在if语句中定义单词的变量

时间:2019-04-13 11:34:45

标签: python python-3.x

我正在尝试了解语句,并希望创建一个可以计算圆形或矩形区域的程序,但是错误不断弹出确定的“矩形” /“圆形”。我尝试了几种方法,但没有任何效果。

我尝试使用引号和单独的变量定义

import math
choice = input("Please enter rectangle or circle")
if 'rectangle' == choice:

    length = input("Please enter length in cm")
    width = input("Please enter width in cm")
    Area = int(length) * int(width)
    print("The area of the square is", Area)
elif choice == 'circle':

    diameter = input("Please enter diameter in cm")
    radius = int(diameter) / 2
    Area = math.pi * (radius ** 2)
    print("The area of the circle is", Area)

我希望能够选择形状并计算面积

1 个答案:

答案 0 :(得分:0)

我唯一想到的问题是,输入形状后,您必须使用多余的空间。例如:“矩形”而不是“矩形”。

尝试在if语句中使用choice.strip(),而不是将其与字符串进行比较。