Python Forum
Money Bags Program - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Money Bags Program (/thread-1742.html)



Money Bags Program - PythonNoob123 - Jan-23-2017

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.

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()



RE: Money Bags Program - Yoriz - Jan-23-2017

coins = ('1p', '2p', '5p', '10p', '20p', '50p', '£1', '£2')
coin_type = input('what type of coin is it?({})'.format(', '.join(coins)))
if coin_type in coins:
    print('{} is an acceptable coin'.format(coin_type))
else:
    print('{} is NOT an acceptable coin'.format(coin_type))



RE: Money Bags Program - PythonNoob123 - Jan-23-2017

(Jan-23-2017, 10:16 PM)Yoriz Wrote: [python]coins = ('1p', '2p', '5p', '10p', '20p', '50p', '£1', '£2')
coin_type = input('what type of coin is it?({})'.format(', '.join(coins)))
if coin_type in coins:
    print('{} is an acceptable coin'.format(coin_type))
else:
    print('{} is NOT an acceptable coin'.format(coin_type))[
I owe you my life.
Check private messages when you can.