我的代码获得了一些输入,并最终输出了一些结果。我已经将a
或ice cream
函数的含义与shortcutA()
关联在一起,将/
的含义与产品分离,将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 / ,并且订单均为20(两者),则预期结果应为:
your code is: ice cream order is 20 sweet ice ice cream order is 20 sweet ice
答案 0 :(得分:0)
据我了解,您对待
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)
。
python 2.x
例如,在print("ice cream"),
中,,
的作用是在打印后不放置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 =" ")