2016-12-20 5 views
1

私は入力を変数に保存し、その下の関数で変数を呼び出すことができました。誰かが "C"、 "c"、 "Circle"、または "circle"を入力した場合など、.upper()と.lower()の選択を考慮したいと思っていました。行う方法がわからない...しかしそのユーザーの入力パラメータを設定し、さまざまな回答を扱う

""" 
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)) 

これは、コードアカデミープロジェクトからだと答えを検索しようとしましたが、ここでは1

答えて

0

はちょうどあなたがユーザーから取得した入力を下げ見つけることができませんでしたそして、小文字に対してのみテスト:

if option.lower() == expected_lowercase_input: 

あなたは、すべての'foo'に低下するなど'FOO''Foo''fOO'、と全くケースを心配する必要はありませんこの方法。 optionの小文字バージョンが'c'または'circle'ある場合ifブロックを実行します

expected = ('c', 'circle') 

if option.lower() in expected: 
    ... 

はあなたのような何かを行うことができ、 'c'または 'circle'のような変動を考慮してください。

Codecademyのものが付いています。それは私の意見では素晴らしいリソースです!