简体   繁体   中英

How to remove parentheses and comma from printed output

Edit: Apparently I was using python 2. Switching to 3 fixed the issue and now I am getting the proper results without the parentheses/commas. Thanks for the replies - problem solved :D

Beginner at Python and coding in general. Struggling with my first project assignment, but I've gotten so close on my own.

My assignment is to create a code in python that counts the number of coins from a given value ie quarters, nickels, dimes, pennies.

My initial code looks like this:

coins=input('Enter amount of change: ')
print("Quarters", coins//25)
coins = coins%25
print("Dimes", coins//10)
coins = coins%10
print("Nickles", coins//5)
coins = coins%5
print('Pennies', coins//1)

Which prompts something like, "Enter amount of change: 86"
('Quarters', 3)
('Dimes', 1)
('Nickles', 0)
('Pennies', 1)

These are the correct values, but my instructor wants it to look like this:

Enter amount of change: 86
Quarters: 3
Dimes: 1
Nickles" 0
Pennies: 1

I can get the colon in there, but how can I remove the parentheses and commas? Thanks

You can use str.format() to produce the required output. For example for quarters:

print('Quarters: {}'.format(coins//25))

This will work in both versions of Python.

The simplest solution I've always used to print values in Python 2, which is the Python version you appear to be using, is the following:

coins=int(input('Enter amount of change: '))
print "Quarters: %i" % (coins//25)
coins = coins%25
print "Dimes: %i" % (coins//10)
coins = coins%10
print "Nickles: %i" % (coins//5)
coins = coins%5
print 'Pennies: %i' % (coins//1)

The % symbol, when used with strings, allows whatever value you want to be printed to be substituted in the string. To substitute multiple values, you separate them with commas. For example:

someInt = 1
someStr = 'print me!'
print "The values are %i and %s" % (someInt, someStr)

This code will substitute in someInt and someStr for %i (used for integers) and %s (used for strings), respectively.

However, the % symbol also functions as the modulus operator, so it does 2 different things when it is being used with strings and when it is being used among two numbers.

Please check :

coins=input('Enter amount of change: ')
print "Quarters:",coins//25
coins = coins%25
print "Dimes:",coins//10
coins = coins%10
print "Nickles:",coins//5
coins = coins%5
print "Pennies:",coins//1

It seems like you are using Python 2. I think you intended to use Python 3 given your use of input() and print() methods, but the code will work in Python 2 by changing print() methods to print keywords . Your code would look like the following in "proper"* Python 2:

coins = input('Enter amount of change: ')
print 'Quarters: ' + str(coins // 25)
coins = coins % 25
print 'Dimes: ' + str(coins // 10)
coins = coins % 10
print 'Nickles: ' + str(coins // 5)
coins = coins % 5
print 'Pennies: ' + str(coins)

Hope this helped!

Footnote: Using % is preferred over using string concatenation, but I still believe that it is easier to read for beginners this way.

To use the print() syntax on python2 add this to the top of your program:

from __future__ import print_function

otherwise python will interpret the argument to print as a tuple and you'll see ().

I am using Python 3 and the following lines exactly give what your instructor wants:

coins=float(input("Enter amount of change: "))
print("Quarters:", round(coins//25))
coins = coins%25
print("Dimes:", round(coins//10))
coins = coins%10
print("Nickels:", round(coins//5))
coins = coins%5
print("Pennies: %.0f" % coins)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM