我设法将输入保存到变量中,然后在它下面的函数中调用它。我想考虑.upper()和.lower()选项,例如是否有人输入" C"," c"," Circle"或&# 34;圆&#34 ;.不知道如何做到这一点......
"""
a calculator that can compute the area of a chosen shape, as selected by the user. The calculator will be able to determine the area of a circle or a triangle
"""
from math import pi
from time import sleep
from datetime import datetime
#opening comments and thinking computer thinks for 1 second
now = datetime.now()
print("Calculator is booting up! Beep Boop...")
print(("%s/%s/%s " + "%s:%s \n") % (now.month, now.day, now.year, now.hour, now.minute))
sleep(1)
#nagging teacher comment if needed
hint = str("Don\'t forget to include the correct units! \nExiting...")
#user input choose Circle or Triangle
option = input("Enter C for Circle or T for Triangle: ")
#Circle computing
if (option.upper() == input(str(C))) or (option.lower() == input(str(c))):
radius = input(float("What is the radius?: "))
area = (pi * radius ** 2)
print("The pie is baking...")
sleep(1)
print(str(area + hint))
来自代码学院的项目并尝试搜索答案,但在这里找不到答案
答案 0 :(得分:0)
只需降低您从用户那里获得的输入,并仅针对小写进行测试:
if option.lower() == expected_lowercase_input:
这样您就不必担心案件'FOO'
,'Foo'
,'fOO'
等等都会降低到'foo'
。
要考虑'c'
或'circle'
等变体,您可以执行以下操作:
expected = ('c', 'circle')
if option.lower() in expected:
...
如果if
的小写版本为option
或 'c'
,则会执行'circle'
阻止。
坚持Codecademy的东西。在我看来,这是一个很好的资源!