2014-11-18 2 views
0

У меня есть программирование с просьбой выбрать из 10 вопросов, на которые они хотят ответить. Для вопросов 2, 4 и 7 у них также есть возможность выбирать между отображением их в метрическом или американском формате. Я смог их полностью преобразовать, но на выходе отображаются нежелательные круглые скобки и запятые, и с тех пор, как я представил преобразованные ответы, я не могу отобразить другие вопросы, если они выбраны.печатные строки и ссылочные переменные перед назначением

программа ---

def main(): 

    print("Hello, here you can choose from these questions, and we'll answer them!") 
    q1 = "How many plastic water bottles do Americans throw away a second?" 
    q2 = "How much food do Americans throw in the trash every year?" 
    q3 = "How many people could be fed with 5 percent of that wasted food?" 
    q4 = "How much waste does the entire U.S. economy create to make a year's worth of food, fuel, resource extraction and products for one American?" 
    q5 = "How much of that total waste figure is recycled?" 
    q6 = "How much energy is wasted on junk mail?" 
    q7 = "How many liters of water are needed to make one liter of bottled water?" 
    q8 = "How much disposable plastic wrap is made each year in America?" 
    q9 = "How many nonrecyclable Styrofoam cups do Americans throw away in a year?" 
    q10 = "How much plastic trash ends up in the ocean?" 

    ans1 = "694" 
    ans2 = "96 billion pounds" 
    ans3 = "4 million for a year" 
    ans4 = "Just under 1 million pounds (waste water not included)" 
    ans5 = "2 percent" 
    ans6 = "One day's worth could heat 250,000 homes." 
    ans7 = "3 liters" 
    ans8 = "Enough to shrink-wrap Texas" 
    ans9 = "25 billion, or enough cups to circle the earth 436 times" 
    ans10 = "The United Nations estimates there are about 46,000 pieces of plastic trash per square mile of ocean, and that 5.6 million tons of trash are dumped, blown or washed into the seven seas." 
    question, answer, choice = getchoice() 

    print(question) 
    print(answer) 
    newanswer = getunits(choice) 
    print(newanswer) 



def getchoice(): 
    print("The questions are: \n1. How many plastic water bottles do Americans throw away a second?\n2. How much food do Americans throw in the trash every year?\n3. How many people could be fed with 5 percent of that wasted food?\n4. How much waste does the entire U.S. economy create to makea year's worth of food, fuel, resource extraction and products for one American?\n5. How much of that total waste figure is recycled?\n6. How much energy is wasted on junk mail?\n7. How many liters of water are needed to make one liter of bottled water?\n8. How much disposable plastic wrap is made each year in America?\n9. How many nonrecyclable Styrofoam cups do Americans throw away in a year?\n10. How much plastic trash ends up in the ocean?") 
    choice = input("Which question would you like the answer for? (type in '1' or '2'..etc): ") 
    while choice != "1" and choice != "2" and choice != "3" and choice != "4" and choice != "5" and choice != "6" and choice != "7" and choice != "8" and choice!= "9" and choice != "10": 
     choice = input("Which question would you like the answer for? (type in '1' or '2'..etc): ") 
    if choice == "1": 
     question = "How many plastic water bottles do Americans throw away a second?" 
     answer = "694" 

    elif choice == "2": 
     question = "How much food do Americans throw in the trash every year?" 
     answer = "" 
    elif choice == "3": 
     question = "How many people could be fed with 5 percent of that wasted food?" 
     answer = "4 million for a year." 
    elif choice == "4": 
     question = "How much waste does the entire U.S. economy create to make a year's worth of food, fuel, resource extraction and products for one American?" 
     answer = "" 
    elif choice == "5": 
     question = "How much of that total waste figure is recycled?" 
     answer = "2 percent" 
    elif choice == "6": 
     question = "How much energy is wasted on junk mail" 
     answer = "One day's worth could heat 250,000 homes." 
    elif choice == "7": 
     question = "How many liters of water are needed to make one liter of bottled water?" 
     answer = "" 
    elif choice == "8": 
     question = "How much disposable plastic wrap is made each year in America?" 
     answer = "Enough to shrink-wrap Texas" 
    elif choice == "9": 
     question = "How many nonrecyclable Styrofoam cups do Americans throw away in a year?" 
     answer = "25 billion, or enough cups to circle the earth 436 times" 
    elif choice == "10": 
     question = "How much plastic trash ends up in the ocean?" 
     answer = "The United Nations estimates there are about 46,000 pieces of plastic trash per square mile of ocean, and that 5.6 million tons of trash are dumped, blown or washed into the seven seas." 

    return question, answer, choice 



def getunits(choice): 
    units = input("Do you want units in Metric or American format? (enter 'm' or 'a') ") 
    if units == "m": 
     if choice == "2": 
     conversion = str(96000000000 * .453592) 
     newanswer = (conversion,"Kilograms") 
     if choice == "4": 
     conversion = str(1000000 * .45392) 
     newanswer = (conversion,"Kilograms") 
     if choice == "7": 
     conversion = str(3 * .26) 
     newanswer = (conversion,"gallons!")  

    return newanswer 



main() 

выход ---

Hello, here you can choose from these questions, and we'll answer them! 
The questions are: 
1. How many plastic water bottles do Americans throw away a second? 
2. How much food do Americans throw in the trash every year? 
3. How many people could be fed with 5 percent of that wasted food? 
4. How much waste does the entire U.S. economy create to makea year's worth of food, fuel, resource extraction and products for one American? 
5. How much of that total waste figure is recycled? 
6. How much energy is wasted on junk mail? 
7. How many liters of water are needed to make one liter of bottled water? 
8. How much disposable plastic wrap is made each year in America? 
9. How many nonrecyclable Styrofoam cups do Americans throw away in a year? 
10. How much plastic trash ends up in the ocean? 
Which question would you like the answer for? (type in '1' or '2'..etc): 2 
How much food do Americans throw in the trash every year? 

Do you want units in Metric or American format? (enter 'm' or 'a') m 
('43544832000.0', 'Kilograms') 

Hello, here you can choose from these questions, and we'll answer them! 
The questions are: 
1. How many plastic water bottles do Americans throw away a second? 
2. How much food do Americans throw in the trash every year? 
3. How many people could be fed with 5 percent of that wasted food? 
4. How much waste does the entire U.S. economy create to makea year's worth of food, fuel, resource extraction and products for one American? 
5. How much of that total waste figure is recycled? 
6. How much energy is wasted on junk mail? 
7. How many liters of water are needed to make one liter of bottled water? 
8. How much disposable plastic wrap is made each year in America? 
9. How many nonrecyclable Styrofoam cups do Americans throw away in a year? 
10. How much plastic trash ends up in the ocean? 
Which question would you like the answer for? (type in '1' or '2'..etc): 9 
How many nonrecyclable Styrofoam cups do Americans throw away in a year? 
25 billion, or enough cups to circle the earth 436 times 
Do you want units in Metric or American format? (enter 'm' or 'a') a 
Traceback (most recent call last): 
    File "assignment6b.py", line 99, in <module> 
    main() 
    File "assignment6b.py", line 36, in main 
    newanswer = getunits(choice) 
    File "assignment6b.py", line 95, in getunits 
    return newanswer 
UnboundLocalError: local variable 'newanswer' referenced before assignment 

ответ

0
def getunits(choice): 
    newanswer ='' 
    conversion = '' 

добавить newanswer в функции вы getunits, он будет локальным для getunits сферы

def getchoice(): 
    question = '' 
    answer = '' 

демо:

>>> x = 10 
>>> def test(): 
...  x += 1 
... 
>>> test() 
Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
File "<stdin>", line 2, in test 
UnbondLocalError: local variable 'x' referenced before assignment 
>>> def test(): 
...  x = 0 
...  print x+1 
... 
>>> test() 
1 
>>> x 
10 
>>> 

Ошибка поднять, потому что х местный функционировать, он не знает о x = 10

так что вы определить внутри функции, ее под сферу функция

вы можете заменить это:

while choice != "1" and choice != "2" and choice != "3" and choice != "4" and choice != "5" and choice != "6" and choice != "7" and choice != "8" and choice!= "9" and choice != "10": 

к:

while choice not in ["1","2","3","4","5","6","7","8","9","10"]: 
+0

надеюсь, что это может помочь вам – Hackaholic

+0

я сейчас возникают проблемы, что в состоянии отображать ответы на 2,4 и 7, используемых в функции получают единицы –

+0

@Johnwayne вы также должны объявить ' преобразование' в функции getunits – Hackaholic

Смежные вопросы