在分配之前打印字符串和引用变量

时间:2014-11-18 00:33:31

标签: python

我有一个编程要求用户从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

1 个答案:

答案 0 :(得分:0)

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

在你的getunits函数中添加一个newanswer,它将是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是函数的本地函数,它不知道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"]: