私のタイトルがこの質問の正義を果たしているかどうかわからない、これは私の最初の投稿です、これがどこかで答えられたら許してください、しかし私は本当にそれを研究して私の質問に固有の答えを見つけようとしました。
私はPythonを初めて使用し、最初のプログラムを作成していました。名前の長さを知りたいかどうかをユーザーに尋ねるコードの部分を除いて、すべてが正常に機能しています。
「はい」と「いいえ」の複数の回答を追加しようとしています。これにより、ユーザーが「はい」や「いいえ」などの入力を行っても、回答を受け入れることができます。考えられる答えのリストを作成しました。ただし、ifステートメントとelifステートメントをスキップして、elseステートメントに直接移動します。また、リストを使用せずに、「and」と「or」を使用してみましたが、うまくいきませんでした。
# prints the length of name if user desires, if not it goes onto ask age
print('Would you like to know the length of your name?')
answer = input()
affirmative = ['yes', 'Yes', 'yeah', 'Yeah', 'yup', 'Yup', 'y', 'Y', 'yea',
'Yea']
negative = ['No', 'no', 'Nope', 'nope', 'N', 'n', 'Nah', 'nah']
if answer == affirmative:
print('The length of your name is ' + str(int(len(myName))))
elif answer == negative:
print('Ok! Thats fine! I didn\'t want to tell you anyway!')
else:
print('Ok then.....next question')
最新バージョンのPythonを使用しています。よろしくお願いします。他の場所で回答があった場合は、もう一度申し訳ありません。
ここにあなたの途中であなたを助けるべきであるいくつかの提案があります。
# prints the length of name if user desires, if not it goes onto ask age
answer = input('Would you like to know the length of your name?')
# Rather than printing, input can take the string to print (helps with figuring out what the input is used for)
affirmative = ['yes', 'Yes', 'yeah', 'Yeah', 'yup', 'Yup', 'y', 'Y', 'yea',
'Yea']
negative = ['No', 'no', 'Nope', 'nope', 'N', 'n', 'Nah', 'nah']
if answer in affirmative:
print('The length of your name is ' + str(int(len(myName))))
elif answer in negative:
print('Ok! Thats fine! I didn\'t want to tell you anyway!')
else:
print('Ok then.....next question')
in
キーワードを使用すると、文字列がリストに含まれているかどうかを確認できます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加