ここ

2017-10-25 2 views
0

である私の現在のコード:ここ

# Imports random 
import random 

def game(): 
    """This holds the function for the game""" 
    # Sets score to 0 intially 
    score = 0 
    wrong = 0 
    # Questions 
    questions = [{"question": "What is the price of a motorcycle?", 
        "answers": ["$1000", "$5000", "$10000", "$15000"], 
        "correct": "2"}, 
       {"question": "How much is this toaster?", 
        "answers": ["$2", "$5", "$7"], 
        "correct": "2"}, 
       {"question": "What is the price of a dog?", 
        "answers": ["$1", "$5000", "$100", "$70"], 
        "correct": "3"}, 
       {"question": "How much is this electric pooper scooper?", 
        "answers": ["$200000", "$90", "$72.99"], 
        "correct": "3"}, 
       {"question": "What is the price of apple sauce?", 
        "answers": ["$.50", "$5", "$3", "$1"], 
        "correct": "4"}, 
       {"question": "is this lamborghini worth $100,000?", 
        "answers": ["True", "False"], 
        "correct": "1"}, 
       {"question": "What is the price of a lifesize manaquin of batman?", 
        "answers": ["$2,530", "$500", "$100", "$45"], 
        "correct": "1"}, 
       {"question": "How much is this 1 night vacation in idaho?", 
        "answers": ["$400", "$1000", "$95"], 
        "correct": "3"}, 
       {"question": "What is the price of a honda Accord?", 
        "answers": ["$1000", "$9500", "$6000", "$18000"], 
        "correct": "4"}, 
       {"question": "is this gold plated microwave worth over $2,000?", 
        "answers": ["True", "False"], 
        "correct": "1"}] 
    # Shuffles questions 
    random.shuffle(questions) 
    print("Welcome to the price is right!") 
    # loop for questions 
    for question in questions: 
     print(question["question"]) 
     for i, choice in enumerate(question["answers"]): 
      print(str(i + 1) + ". " + choice) 
     answer = input("Choose an answer: ") 
     if answer == question["correct"]: 
      print("That is correct!") 
      score = score + 1 
     else: 
      print("That answer is incorrect!") 
      wrong = wrong + 1 
    # Score + Thank you message 
    print() 
    print() 
    print("Your total score is:", score, "right, and", wrong, "wrong.") 
    print("Thanks for playing the price is right!") 
    print() 
    main() 

def main(): 
    """Calls all options""" 
    while True: 
     print("Welcome to the Price is Right! I'm your host, Python! What would you like to start with?!") 
     print() 
     option = input("Play, View Credits, or Quit:") 
     if option.lower() == "play": 
      return game() 
     elif option.lower() == "view credits": 
      print("Created by: Gennaro Napolitano and Mario DeCristofaro") 
     elif option.lower() == "quit": 
      exit() 
     else: 
      False 
      print() 
      print("Sorry, that is not a valid input, please try again!") 
      print() 

# Calls main 
if __name__ == '__main__': 
    main() 

ゲームは(1、2または3を選択することで、正しい答えを選択するようにユーザーをしている実行している基本的に一度かそこにいくつの答えがあるか)。

質問回答に間違ったオプションを入力すると、入力を促すことができます。例えば。

質問:

How much does this toaster cost?: 
1. $2 
2. $3 
3. $4 

Usern入力:

A 

プログラムの応答:

Invalid response, please try again(Please choose "1", "2", or "3") 

そして、それは質問を中継し、ユーザに再入力するには、別の機会を与えるだろう答え。

answer = input("Choose you answer: ") 
while answer not in ['1', '2', '3']: 
    print("Invalid response, please try again(Please choose '1', '2', or '3'") 
    answer = input("Choose you answer: ") 

したがって、次のコードを使用して

(例えばいくつかの質問には2つの潜在的な答えを持っている、いくつかは...など、5を持っている)可能性のある回答数は、ほとんどの質問のために異なっていることに注意してください

潜在的に3つの回答のセットリストがあるため、うまくいかないでしょう。私は苦労している具体的な質問の特定の量の回答には疎遠でなければなりません。

ありがとうございました!

答えて

0

「自分自身を繰り返さないでください」。これは、回答リストをすでにdictに保存しているためです。

for q in questions: 
    # q['answers'] is the array of possible answers for each question q 
    print(q['question']) 
    answer = input("Choose you answer: ") 
    while answer not in q['answer']: 
     print("Invalid response, please try again(Please choose" + 
      ','.join('"'+ a + '"' for a in q[answer])