2013-10-31 120 views
0

我正在为我正在编写的一个小程序编写一部分代码。记住我对此很新。python3循环问题

继承人的代码:

def sell(): 
    sell = input("\nGreetings! What would you like to do today?\nPress 1 to sell an animal\nPress 2 to buy an animal\nPress 3 If you want to see all the farms and their animals first\n") 

    if sell == "1": 
     whichs = input("Which animal do you want to sell?\nPress 1 for pig\nPress 2 for horse\nPress 3 for cow\nPress 4 for bull\n") 
     if whichs == "1": 
      print ("\nYou just sold\n",p[0]) 
      print ("\nYou now have 350gold") 
      print ("\nThese are the animals you have left:") 
      print (p[1], p[2], p[3]) #Prints the animals you have left from p list. 
     elif whichs == "2": 
      print ("\nYou just sold\n",p[1]) 
      print ("\nYou now have 350gold") 
      print ("\nThese are the animals you have left:") 
      print (p[0], p[2], p[3]) 
     elif whichs == "3": 
      print ("\nYou just sold\n",p[2]) 
      print ("\nYou now have 360gold.") 
      print ("\nThese are the animals you have left:") 
      print (p[0], p[1], p[3]) 
     elif whichs == "4": 
      print ("\nYou just sold\n",p[3]) 
      print ("\nYou now have 350gold.") 
      print ("\nThese are the animals you have left:") 
      print (p[0], p[1], p[2]) 
     else: 
      print ("Error") 

我想这个循环因此当用户已经卖出一只动物,他们开始了与:

sell = input("\nGreetings! What would you like to do today?\nPress 1 to sell an animal\nPress 2 to buy an animal\nPress 3 If you want to see all the farms and their animals first\n") 

我正与如何struggeling已经完成了。

+2

你试过'while'语句吗? –

+1

这里是[参考](http://docs.python.org/2/reference/compound_stmts.html#while)。这里是一个[教程](http://www.tutorialspoint.com/python/python_while_loop.htm)。 –

回答

2

其他两个答案是正确的告诉你使用while循环,但未能解决一个更一般的问题:循环不应该sell功能但它的外面,如您文字表明用户也可以购买东西或查看他的统计数据。你应该为所有这些行动的创建单独的函数,然后创建一个循环来检查行动,并调用相应功能:

def buy(): 
    #... 

def sell(): 
    #... 

def stats(): 
    #... 

while True: 
    choice = input("1: Buy 2:Sell 3:Stats - press any other key to exit") 
    if choice == "1": buy() 
    elif choice == "2": sell() 
    elif choice == "3": stats() 
    else: break 

循环可以通过使用类似的选择映射到功能更Python的方法进行优化在字典中,但为了清晰起见,我已经写了一个简单的例子。另外,如果你不选择把所有的状态都保存在全局变量中(你不应该这样做),那么将函数放入一个同时包含当前余额,股票和其他游戏的类中也是有意义的参数。

+0

非常感谢你,我一直在想如果最好在一个函数中包含所有代码。现在我的代码看起来更干净,一切都很好!我不太清楚你在绘制选项时的意思。如果这就是你的意思,我会为所有的农场和动物类型使用类。 – Bondenn

0

尝试使用while循环:

def sell_function(): 
    while True: 
     sell = input("\nGreetings! What would you like to do today?\nPress 1 to sell an animal\nPress 2 to buy an animal\nPress 3 If you want to see all the farms and their animals first\n") 
     # ... 
      else: 
       print("Error") 
       break  # Stop looping on error 

我们也可以设置为True一个变量,做while variable然后variable = False,而不是break达到相同的效果(在这种情况下)。

我重命名了你的函数,因为你使用了一个叫做sell的变量,并且有一个可能导致问题的函数。另外,您后面无疑会找到有用的breakcontinue声明。

+0

在某处添加“break”可能是一个好主意。 – uselpa

+0

休息时间应该在哪里才能让循环工作并在正确的地方出去?我试过在else语句之后放置这个break,但它没有奏效。 – Bondenn

+0

@ user2877270看到我的回答,如果你想打破错误,它应该直接在'print(“Error”)'部分的下面(并且在相同的缩进级别)。 – rlms

1
def sell(): 
    looping = True 

    while looping: 
     sell = input("\nGreetings! ... Press Q for quit") 

     if sell == "1": 
      #rest of your code 
     elif sell == "Q": 
      looping = False