1

私はクイズを実行する必要がある課題を行っています。これまでのところ、これが私のコードです。

print("Hello and welcome to Shahaad's quiz!") #Introduction
name = input("What is your name? ")
print("Alright", name,", these will be today's topics:")
print("a) Video Games")
print("b) Soccer")
print("c) Geography") 
choice = input("Which topic would you like to begin with?")
if choice == 'video games'
    print("Lets start with Video Games!")

人が最初のトピックとしてビデオゲームを選択した場合、最後の行が出力されるようにしようとしていますが、if choice == 'video games' でエラーが発生し続けます。

4

2 に答える 2

4

スタックオーバーフローへようこそ。あなたはとても近いです!

次のように、if ステートメントの最後にコロンが必要です。

if choice == 'video games':
    print("Lets start with Video Games!")

ブロックを開く Python のすべてのもの:forループ、whileループ、ifステートメント、関数def開始などの後にはコロンが必要です。

しかし、ユーザーが別のケース ( ViDeO GaMeS) で入力した場合はどうなるでしょうか。念のため小文字に変換してみましょう。

if choice.lower() == 'video games':
    print("Let's start with Video Games!")
于 2013-10-25T03:20:11.377 に答える
0

上記の回答で述べたように、コロンを使用する必要があります。さらに、間違った値と比較しています: ユーザーはVideo Gamesnotと入力video gamesするか、選択肢全体を入力する可能性があります: a) Video Games。したがって、選択した単語が単語の全体または一部ではなく、入力に含まれているかどうかを常に確認する必要があります-

choice = "a) Video games"
if "Video games" in choice:
    print("You selected (a)")
于 2013-10-25T04:22:27.733 に答える