2016-11-25 6 views
0

PythonのBootcampの場合、私は繰り返し「名前」の入力を求めてプリントアウトするプログラムに取り組んでいます。エラーのメッセージを繰り返して出力します

ユーザーが「bob」と入力すると、プログラムは「oh、not you、bob!」を印刷し、以前入力した最短および最長の名前を印刷する必要があります。

ユーザーが文字列(数字など)以外のものを入力した場合は、エラーメッセージが表示され、名前をもう一度尋ねる必要があります。

`new_name = '' 
    while new_name != 'bob': 
    #Ask the user for a name. 
    new_name = input("Please tell me someone I should know, or enter 'quit': ") 
    print('hey', new_name, 'good to see you') 

    if new_name != 'bob': 
    names.append(new_name) 

    largest = None 
    for name in names: 
    if largest is None or len(name) > len(largest) : 
    largest = name 

    smallest = None 
    for name in names: 
    if smallest is None or len(name) < len(smallest) : 
    smallest = name 

    print ('oh, not you, bob') 
    print ("The smallest name previously entered is :", smallest) 
    print("The largest name previously entered is :", largest) 

私は、ユーザーが「ロミオ」のような文字列よりもint型、float型、または何か他のものを入力したとき

私のプログラムは、以下を参照してくださいエラーメッセージをプリントアウトする方法がわかりません

あなたの助けのために非常に多くの

答えて

1

をいただき、ありがとうございます、それはその数を動作するかどうintにご入力を変換するようにしてください。

try: 
    user_number = int(input("Enter a name: ")) 
except ValueError: 
    print("That's a good name!") 
ユーザ入力が文字だけが含まれている場合
1

あなたがチェックできます。

if not new_name.isalpha(): 
    print 'Only letters are allowed!' 

注:空白も禁じ文字として扱われます。

関連する問題