2015-10-04 184 views
0

我正在寫一個程序,使用50個州的字典。這個程序。會詢問用戶大約8個問題,在用戶回答問題後,會出現一些問題,比如「根據你的答案,你應該生活在這個狀態」。它是一個隨機輸出的狀態。問題會循環,直到用戶決定停止。這是我迄今爲止所做的。你能幫我嗎?謝謝蟒蛇字典的國家

import random 
def main(): 
     states = { 

     'Alabama','Alaska','Arizona','Arkansas','California','Colorado', 
     'Connecticut','Delaware','Florida','Georgia','Hawaii','Idaho', 
     'Illinois','Indiana','Iowa','Kansas','Kentucky','Louisiana', 
     'Maine' 'Maryland','Massachusetts','Michigan','Minnesota', 
     'Mississippi', 'Missouri','Montana','Nebraska','Nevada', 
     'New Hampshire','New Jersey','New Mexico','New York', 
     'North Carolina','North Dakota','Ohio',  
     'Oklahoma','Oregon','Pennsylvania','Rhode Island', 
     'South Carolina','South Dakota','Tennessee','Texas','Utah', 
     'Vermont','Virginia','Washington','West Virginia', 
     'Wisconsin','Wyoming' 
    } 


    print('What city are you from') 
    city = input() 
    print('What is your favorite team?') 
    team = input() 
    print('What state is close to you?') 
    state = input() 
    print('What is the name of your Governor?') 
    governor = input() 
    print('What is the name of your Senator?') 
    senator = input() 
    print('what is the name of your Sherif?') 
    sherif = input() 
    print('What is your favorite baseball team?') 
    baseball = input() 
    print('What is your favorite basketball team?') 
    basketball = input() 
    print('What is your favorite hockey team?') 
    hockey = input() 

    print ('Base on your answer the state you should live in is:' + states) 
+0

這是一組,嘗試'型(州)' – garg10may

+0

正是如何或你在哪裏卡住了?你需要哪些幫助?你在尋找一對結對的伴侶嗎? – WhiteViking

+0

我有這個工作。至於你的第三個問題,我確實需要一個編程夥伴。我真的想從-z – jay

回答

0

首先,您提供的狀態不是字典而是集合。請嘗試:

type(states) 

用於隨機選擇。首先將其轉換爲列表,然後選擇如下。

import random 
states_list = list(states) 
choice = random.choice(states_list) 

因此,在最後一行包括

print ('Base on your answer the state you should live in is: ' + choice) 
+0

知道這個python,你說我提供的狀態不是字典。我錯過了什麼? – jay

+0

字典是鍵值/對的形式。像{1:'alabama'},但爲了你的目的,你爲什麼需要一本字典。你可以從列表中隨機選擇。 – garg10may

+0

如何將其轉換爲字典而不是集合? – jay