Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Money Bags Program
#1
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()
Reply
#2
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))
Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have this coding exercise to find how much money take Barrack Obama and Romney Mitt vasiliskarv 1 306 Mar-19-2024, 08:24 PM
Last Post: deanhystad
  Banking system - transferring money 3DEN 2 8,867 Dec-13-2019, 09:13 AM
Last Post: 3DEN
  Money Global Variable saturnstars 8 4,273 Apr-12-2019, 10:48 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020