我正在尝试使用python中的GTIN-8制作购物清单。我的问题是我似乎无法找到一种方法让用户输入一个数字(来自下面的选项的GTIN-8代码),然后让程序获取该数字,在给定的数据库中搜索它然后显示该数字等于的项目。
例如,如果用户输入12345670 2
,程序将输出
12345670 Hammer 2 4.50
Subtotal 9.00
我的代码如下:
print("Hi There! Welcome to sSpecialists!")
print("To start shopping, note down what you want to buy and how much of it")
print("Here are the purchasable items")
print("~~~~~")
print("12345670 is a hammer (£4.50)")
print("87654325 is a screw driver (£4.20)")
print("96385272 is a pack of 5 iron screws (£1.20)")
print("74185290 is pack of 20 100mm bolts (£1.99)")
print("85296374 is a pack of 6 walkers crisps (£1)")
print("85274198 is haribo pack (£1)")
print("78945616 is milk (£0.88)")
print("13246570 is a bottle of evian water (£0.99)")
print("31264570 is kitkat original (£0.50)")
print("91537843 is a cadbury bar (£1)")
print("~~~~~")
response = input("To view more of our items type more, else no: ")
if response == 'more':
print("~~~~~")
print("45762147 is a 20 pack of sausages (£2.49)")
print("19348629 is a toy lion (£1.20)")
print("34821565 is a pack of 7 beef burgers (£1.20)")
print("48613545 is pack of 10 fish fingers (£1.99)")
print("46729811 is 500ml of heinz mayonnaise (£1)")
print("48613255 is 500ml of heinz ketchup (£1)")
print("31750677 is youghurt (£0.88)")
print("13246570 is 500ml of tropical juice (£0.99)")
print("76136948 is adult bmx bike (£499)")
print("76153242 is a sony ps4 (£249)")
print("46821799 is adult bmw i8 (£89,000)")
print("01352474 is a jar of pickles (£1)")
print("98523153 is a 20 pack of adult diapers (£7)")
print("18648515 is £20 psn card (£19.99)")
if response == 'no':
print(" ")
print("Alright, now start typing what you want to order")
print("Include the amount with a spacing")
print("For example 12345670 2 would be two hammers")
print("And when you're done, type 'END' to finish")
print("Start your orders")
print(" ")
full_list = " "
orders = []
while full_list != "END":
full_list = input("Enter your order: ")
if full_list != "END":
orders.append(full_list)
我需要输入获取用户输入的GTIN代码并将其作为书面项目转换回用户的方法。这是Python 3.5。
答案 0 :(得分:-1)
BOARD_SIZE = 8
class BailOut(Exception):
pass
def validate(queens):
left = right = col = queens[-1]
for r in reversed(queens[:-1]):
left, right = left-1, right+1
if r in (left, col, right):
raise BailOut
def add_queen(queens):
for i in range(BOARD_SIZE):
test_queens = queens + [i]
try:
validate(test_queens)
if len(test_queens) == BOARD_SIZE:
return test_queens
else:
return add_queen(test_queens)
except BailOut:
pass
raise BailOut
queens = add_queen([])
print queens
print "\n".join(". "*q + "Q " + ". "*(BOARD_SIZE-q-1) for q in queens)
33 lines: "Guess the Number" Game (edited) from http://inventwithpython.com
import random
guesses_made = 0
name = raw_input('Hello! What is your name?\n')
number = random.randint(1, 20)
print 'Well, {0}, I am thinking of a number between 1 and 20.'.format(name)
while guesses_made < 6:
guess = int(raw_input('Take a guess: '))
guesses_made += 1
if guess < number:
print 'Your guess is too low.'
if guess > number:
print 'Your guess is too high.'
if guess == number:
break
if guess == number:
print 'Good job, {0}! You guessed my number in {1} guesses!'.format(name, guesses_made)
else:
print 'Nope. The number I was thinking of was {0}'.format(number)
Ur wlcm