0

so im learning how to programme with python, started recently therefor, i still suck! there is this exercise that asks me to create a programme that ads up your change , asking about 5c,10c,20c and 50c coins.. I dont know why its not working the total is ridiculously high, can anyone please help?

print "Hello mate, this programme helps you calculate the amount of small change you",
print "carry in Euros, if you are too dumb to count it on your own or",
print "just too lazy, this is the programme for you!!"


q=10*raw_input("how many 10c coins do you have?")
d=20*raw_input("how many 20c coins do you have?")
n=5*raw_input("how many 5c coins do you have?")
p=50*raw_input("how many 50c coins do you have?")`enter code here`
tc=int(q+d+n+p)

print "your total change is",tc,"thank you for choosing this programme!" 

6 Answers6

1

Python has two functions for reading user input, called input and raw_input. The raw_input does not evaluate the data and returns it as it is, in a string format. Whereas the input function evaluates the value. So, for your inputs to be recogonized as integers, i suggest you use

q = 10 * input("how many 10c coins do you have?")

for more detailed explaination about it, please refer How can I read inputs as integers?

or refer the python docs https://docs.python.org/2/library/functions.html#input

Community
  • 1
  • 1
0

raw_input reads your numbers in as strings, so you need to convert them to integers before multiplying them. The multiplication you are doing is repeating the number the number of times equal to the value of the coin.

cr1msonB1ade
  • 1,716
  • 9
  • 14
0

you have to add int(raw_input("..."))

print "Hello mate, this programme helps you calculate the amount of small change you",
print "carry in Euros, if you are too dumb to count it on your own or",
print "just too lazy, this is the programme for you!!"


q=10*int(raw_input("how many 10c coins do you have?"))
d=20*int(raw_input("how many 20c coins do you have?"))
n=5*int(raw_input("how many 5c coins do you have?"))
p=50*int(raw_input("how many 50c coins do you have?"))
tc=int(q+d+n+p)

print "your total change is",tc,"thank you for choosing this programme!" 
Mir Ilias
  • 475
  • 3
  • 9
0

This line:

raw_input("how many 10c coins do you have?")

returns a string. In python, the product of an int, x, and a string, s, is that string repeated x times. You want to cast the output of raw_input to int before multiplying.

Will Pan
  • 282
  • 1
  • 10
0

You are multiplying the string input by 10, 20, 5, and 50 so if all your input are 1 you will get 85 1's in a string. What you need is to cast the variables to integers before multiplying like so:

q=10*int(raw_input("how many 10c coins do you have?"))
d=20*int(raw_input("how many 20c coins do you have?"))
n=5*int(raw_input("how many 5c coins do you have?"))
p=50*int(raw_input("how many 50c coins do you have?"))
tc=(q+d+n+p)
Alex Ramos
  • 135
  • 2
  • 14
0

to the op, this is the code I've written...

quarterCount = float(raw_input("User, please enter the number of Quarters you have: "))
dimeCount = float(raw_input("User, please enter the number of Dimes you have: "))
nickleCount = float(raw_input("User, please enter the number of Nickles you have: "))
pennyCount = float(raw_input("User, please enter the number of Pennies you have: "))

then I assigned values to the next variables I used to float for the coins such as...

quarterCountProp = 0.25 * float(quarterCount)
print "You have $",
print "{0:.2f}".format(quarterCountProp),
print "worth of Quarters."

at the end of the program, in order to get the grand total I did...

grandTotal = quarterCountProp + dimeCountProp + nickleCountProp + pennyCountProp
print "Your total pocket change is: $",
print "{0:.2f}".format(grandTotal)