Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x python新手,如何将用户输入添加到一起_Python 3.x - Fatal编程技术网

Python 3.x python新手,如何将用户输入添加到一起

Python 3.x python新手,如何将用户输入添加到一起,python-3.x,Python 3.x,我对编码非常陌生,在学习了HTML和CSS的所有语法之后,我想我会转向python,但实际上我会学习如何使用它成为python程序员,而不仅仅是学习语法 因此,作为我的第一个小项目之一,我想我会制作一小段代码,要求用户购买一些他们正在购买的物品,然后将这些物品的价格加在一起,给出他们购物清单的价格,但实际上,我无法让它工作(p.s.我真的很新,所以我的代码可能看起来很愚蠢) 我建议搜索python基础知识和循环 这会成功的 # items and their prices items = {'b

我对编码非常陌生,在学习了HTML和CSS的所有语法之后,我想我会转向python,但实际上我会学习如何使用它成为python程序员,而不仅仅是学习语法

因此,作为我的第一个小项目之一,我想我会制作一小段代码,要求用户购买一些他们正在购买的物品,然后将这些物品的价格加在一起,给出他们购物清单的价格,但实际上,我无法让它工作(p.s.我真的很新,所以我的代码可能看起来很愚蠢)


我建议搜索python基础知识和循环

这会成功的

# items and their prices
items = {'banana': 3,
         'apple': 2,
         'milk': 12,
         'bread': 15,
         'orange': 3,
         'cheese': 14,
         'chicken': 42,
         'peanuts':32 ,
         'butter': 20,
         'cooldrink': 18,
         }
#Asking for the item

price = 0
while True:
    choice = input("Select your item(type 'nothing' to make it stop) \n : ")

    if choice == 'nothing':
        break

    if choice in items.keys():
        price += items[choice]
    else:
        print("Uh oh, I don't know about that item")

print('your result', price )


你的代码有问题吗-

if choice in items:
        the_choice = price[choice]
在上面的代码行中,您试图做的事情与您试图实现的事情没有任何意义。您可能希望将用户输入的每个项目添加到
price
列表中。但是您将选择索引处的值赋给了\u choice变量,这是错误的


您试图做的事情可以通过以下方式完成-

# items and their prices
items = {'banana': 3,
         'apple': 2,
         'milk': 12,
         'bread': 15,
         'orange': 3,
         'cheese': 14, 
         'chicken': 42,
         'peanuts':32 ,
         'butter': 20,
         'cooldrink': 18,
         }
#Asking for the item
choice = input("Select your item(type 'nothing' to make it stop) \n : ")

#the list the purchased items would go into.
# If user enters input not in items, it would add 0 into price
price = [items.get(choice,0)]

# The loop will keep running until user enters nothing and it stops looping
while True:
    choice = input('Another item?: ')
    if choice in items:
        # Append price of each choice of items that user inputs
        price.append(items[choice])
    # Break whenever user enters nothing.
    elif choice == 'nothing':
        break
    # On any other input, just print that you don't know about that item
    else:
        print("Uh oh, I don't know about that item")
# At last, printing the final cost of all the items that user selected
print('Your total cost of items is - ',sum(price))
输出:

Select your item(type 'nothing' to make it stop) 
 : milk
 Another item?: bread
 Another item?: butter
 Another item?: nothing
 Your total cost of items is -  47
另外,请注意,除非您要向用户打印价目表,否则不需要维护价目表。您可以只保留一个变量,将所有项目的价格总和相加,并在最后打印出来。这取决于您试图用代码实现的目标。

这就是您想要的吗

# items and their prices
items = {'banana': 3,
         'apple': 2,
         'milk': 12,
         'bread': 15,
         'orange': 3,
         'cheese': 14, 
         'chicken': 42,
         'peanuts':32 ,
         'butter': 20,
         'cooldrink': 18,
         }



price = 0

while True:
    choice = input("Select your item(type 'nothing' to make it stop) \n : ")
    if choice in items:
        price += items[choice]
    elif choice == 'nothing':
        break
    
    else:
        print("Uh oh, I don't know about that item")
print('Your total price is: ',price)
代码: 输出: 解释
users\u items
是一个列表。当用户键入他的选择时,程序会附加到一个元组列表中(项目、价格)。在
nothing
之后,循环结束并打印结果
final_cost
用户项中每个元组的第二个元素之和
final_items
用户_items中每个元组的第一个元素列表

@Vasyl Yovdiy有一个最简单的答案,初学者很容易理解。但是,它缺少后续输入的不同字符串要求。我将首先添加一个变量
,它控制输入的字符串

# items and their prices
items = {'banana': 3,
         'apple': 2,
         'milk': 12,
         'bread': 15,
         'orange': 3,
         'cheese': 14,
         'chicken': 42,
         'peanuts':32 ,
         'butter': 20,
         'cooldrink': 18,
         }
#Asking for the item

price = 0
first = True
while True:
    
    if first:
        choice = input("Select your item(type 'nothing' to make it stop) \n : ")
        first = False
    else:
        choice = input('Another item?: ')

    if choice == 'nothing':
        break

    if choice in items.keys():
        price += items[choice]
    else:
        print("Uh oh, I don't know about that item")

print('your result', price )

这就是你要找的吗

# items and their prices
items = {'banana': 3,
         'apple': 2,
         'milk': 12,
         'bread': 15,
         'orange': 3,
         'cheese': 14, 
         'chicken': 42,
         'peanuts':32 ,
         'butter': 20,
         'cooldrink': 18,
         }
#Asking for the item
choice = input("Select your item(type 'nothing' to make it stop) \n : ")
a = input('Would you like to get another item? (yes/no) > ')
b = print("See you again!")
c = input('Another item? >')
#the list the purchased items would go into
price = []
if (choice == 'nothing'):
    print(b)
    quit()
else:
    print(c)
    if (c != items):
        print("Sorry, I don't know that item")
        print(a)
    if (a == 'yes'):
        input('what is it? > ')
    else:
         print(b)
         quit(all)

如果是的话,欢迎您,如果不是的话,请您再给我解释一下好吗?

您遇到了什么问题?它会给出错误,因为项目不是迭代器。如果用户给出的第一个项目不是
什么都不是
,它将变成无限循环。逻辑是正确的,但OP要求后续输入是
输入('other item?:')
。谢谢,非常有用,我还在学习循环,所以我还是很熟悉的。是的,我还是新的,循环可能会变得有点复杂,但我仍在尝试,谢谢你的帮助!!!非常感谢,我把while循环复杂化了,我应该使用条件句,直到我掌握loops@ShadowNinja,很乐意帮忙:)!好的,很好,谢谢你现在说得通了,你解释得很好,谢谢:)很高兴知道它有帮助:)哦,好的,很好,真的很有帮助
Select your item(type 'nothing' to make it stop) 
 : banana
Select your item(type 'nothing' to make it stop)
 : nothing
Item not in item's list
Final sum of users items is: 3. Items ['banana']
# items and their prices
items = {'banana': 3,
         'apple': 2,
         'milk': 12,
         'bread': 15,
         'orange': 3,
         'cheese': 14,
         'chicken': 42,
         'peanuts':32 ,
         'butter': 20,
         'cooldrink': 18,
         }
#Asking for the item

price = 0
first = True
while True:
    
    if first:
        choice = input("Select your item(type 'nothing' to make it stop) \n : ")
        first = False
    else:
        choice = input('Another item?: ')

    if choice == 'nothing':
        break

    if choice in items.keys():
        price += items[choice]
    else:
        print("Uh oh, I don't know about that item")

print('your result', price )
# items and their prices
items = {'banana': 3,
         'apple': 2,
         'milk': 12,
         'bread': 15,
         'orange': 3,
         'cheese': 14, 
         'chicken': 42,
         'peanuts':32 ,
         'butter': 20,
         'cooldrink': 18,
         }
#Asking for the item
choice = input("Select your item(type 'nothing' to make it stop) \n : ")
a = input('Would you like to get another item? (yes/no) > ')
b = print("See you again!")
c = input('Another item? >')
#the list the purchased items would go into
price = []
if (choice == 'nothing'):
    print(b)
    quit()
else:
    print(c)
    if (c != items):
        print("Sorry, I don't know that item")
        print(a)
    if (a == 'yes'):
        input('what is it? > ')
    else:
         print(b)
         quit(all)