首页 > 解决方案 > 在我的 for 循环中无法达到预期的打印结果

问题描述

我的代码得到一些输入并最终打印出一些结果。我与a意义ice creamshortcutA()功能、/产品的意义分离以及everything else目前相关联unrecognized。之后,将要求用户输入每个产品的数量,并将打印出他输入的总和,并对打印的输出进行一些修改。

sentences = str(input("please enter the password :"))

for i in sentences:
  if i == "/":
    howMuchOrder = input("please enter the order :")
  else:
    continue

class meaning():
  def shortcutA (self):
    global sentences
    print ("ice cream")
    for i in sentences:
        if i != "/":
          print ("sweet ice") 
        elif i =="/":
          print ('order is ' + str(int(howMuchOrder)))

def main():
    m = meaning()
    print_flag = False
    for i in sentences :
        if i in ['a', 'b', '/'] and not print_flag:
          print("your code is: ")
          print_flag = True
        if i == "a" :
          m.shortcutA()
        elif i == "/":
          break
        else :
             print ("unrecognized")


if __name__ == "__main__":
  main()

假设输入只是单词a那么结果将是:您的代码是:冰淇淋甜冰

假设输入只是单词a/那么结果将是:您的代码是:冰淇淋订单是 20 甜冰

如果输入是a/a/,并且 order 是 20(对于两者),那么期望的结果应该是:

your code is: ice cream order is 20 sweet ice ice cream order is 20 sweet ice

标签: python

解决方案


据我了解,您对待:

  • a快捷方式为ice cream
  • whatever else作为一个unrecognized

然后对于shortcutA您想要打印的ice cream order is <order_amount> sweet ice

基于这些理解并且假设a/a/你期望ice cream order is 20 sweet ice ice cream order is 20 sweet ice我已经修改了你的一些逻辑和一些部分但不是所有内容来向你展示如何实现你想要的输出(在 python 2.x 中工作,最后看看如何使它工作在 python 3.x 中)

# now input() works in same way on either python 2.x or 3.x
try:
    input = raw_input
except NameError:
    pass

sentences = str(input("please enter the password :"))

# split sentences by '/', remove empty values, put result in a list
listOrderName = list(filter(None, sentences.split('/')))
totalOrder = sentences.count('/')
listOrderAmount = [0]*totalOrder

if totalOrder > 0:
    for index, i in enumerate(listOrderAmount):
        howMuchOrder = input("please enter the order :")
        listOrderAmount[index] = howMuchOrder

#Exmple of what are your two input
print(listOrderName) #can be deleted
print(listOrderAmount) #can be deleted

class meaning():
  def shortcutA (self, position):
    global listOrderAmount, totalOrder

    print("ice cream"),

    if totalOrder > 0:
        totalOrder = totalOrder - 1
        amount = listOrderAmount[position]
        print('order is ' + str(int(amount))),

    print("sweet ice")

  def shortcutUnrecognized(self):
    print("unrecognized")

def main():
    m = meaning()
    print_flag = False
    position = -1

    global listOrderName
    for order in listOrderName :
        position += 1
        if order in ['a', 'b'] and not print_flag:
          print("your code is: ")
          print_flag = True
        if order == "a" :
          m.shortcutA(position)
        else :
          m.shortcutUnrecognized()  


if __name__ == "__main__":
  main()

使用示例:

#please enter the password :a/a/
#please enter the order :20
#please enter the order :20
#['a', 'a']
#['20', '20']
#your code is: 
#ice cream order is 20 sweet ice
#ice cream order is 20 sweet ice

另一个使用示例:

#please enter the password :a/b
#please enter the order :20
#please enter the order :20
#['a', 'b']
#['20', '20']
#your code is: 
#ice cream order is 20 sweet ice
#unrecognized

另一个使用示例:

#please enter the password :a/a
#please enter the order :20
#['a', 'a']
#['20']
#your code is: 
#ice cream order is 20 sweet ice
#ice cream sweet ice

您可以删除对您无用的打印件(例如print(listOrderName)print(listOrderAmount).

print("ice cream"),例如,python 2.x 用于,在打印后不放置 EOL(行尾或换行),因此我们在同一行中进行了一些打印,而在新行中进行了其他一些打印。这适用于python 2.x

python 3.x 要实现与中相同的行为print(..),python 3.x您可以替换print("ice cream"),为例如print("ice cream", end =" "). 因此,本质上要使脚本以与 python 3.x 中的打印部分相同的方式工作,请更改以下内容:

print("ice cream"),
print('order is ' + str(int(amount))),

进入这个:

print("ice cream", end =" ")
print('order is ' + str(int(amount)), end =" ")

推荐阅读