Jan-23-2017, 08:06 PM
Hi all,
I need some help with adding some validation into my program. I need to make sure that the user cannot enter no prohibited characters into the inputs. For example, my program asks what coin type the user would like to use, the user should only be able to answer '1p', '2p' etc.
Thanks in advance.
I need some help with adding some validation into my program. I need to make sure that the user cannot enter no prohibited characters into the inputs. For example, my program asks what coin type the user would like to use, the user should only be able to answer '1p', '2p' etc.
Thanks in advance.
startMenu = open('Menu.txt','r') print(startMenu.read()) startMenu.close() count = 0 def check(bag_weight, coin_type, bag_weight1): global count count = count+1 x = bag_weight1[coin_type] y = x[1] if bag_weight > y: print("you have",int(bag_weight - y), "too many coins") else: print("you need to add",int(y - bag_weight), "coins") def main(): while True: print("you have checked", count, "bags.") y = input('do you want to check a bag?(Y/N)') if y.lower() == 'y': weight = float(input('how heavy is the bag(grams)?')) coin_Weight = {'1p' :[3.56, 100],'2p':[7.12, 50],'5p':[3.25,100],'10p':[6.5, 50],'20p':[5.0, 50],'50p':[8.00, 20],'£1':[9.50, 20],'£2':[12.00, 10]} coin_type = input('what type of coin is it?(1p, 2p, 5p, 10p, 20p, 50p, £1, £2)') bag_weight1 = coin_Weight[coin_type] bag_weight = weight/bag_weight1[0] print('there are', int(bag_weight), coin_type, 'coins in the bag.') check(bag_weight, coin_type, coin_Weight) else: print("Thank you for using this program") break main()