简体   繁体   English

如何从打印输出中删除括号和逗号

[英]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. 编辑:显然我正在使用python 2.切换到3修复了问题,现在我得到了正确的结果没有括号/逗号。 Thanks for the replies - problem solved :D 谢谢你的回复 - 问题解决了:D

Beginner at Python and coding in general. Python的初学者和一般的编码。 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. 我的任务是在python中创建一个代码,用来计算给定值的硬币数量,即季度,镍币,硬币,硬币。

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" 提示类似“输入更改量:86”
('Quarters', 3) ('宿舍',3)
('Dimes', 1) ('Dimes',1)
('Nickles', 0) ('Nickles',0)
('Pennies', 1) ('便士',1)

These are the correct values, but my instructor wants it to look like this: 这些是正确的值,但我的教师希望它看起来像这样:

Enter amount of change: 86 输入更改量:86
Quarters: 3 宿舍:3
Dimes: 1 尺寸:1
Nickles" 0 尼克斯“0
Pennies: 1 便士: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. 您可以使用str.format()来生成所需的输出。 For example for quarters: 例如季度:

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

This will work in both versions of Python. 这适用于两个版本的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: 我一直用于在Python 2中打印值的最简单的解决方案,即您似乎正在使用的Python版本,如下所示:

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. 此代码将分别替换someIntsomeStr中的%i (用于整数)和%s (用于字符串)。

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 . 看起来你正在使用Python 2.我认为你打算使用Python 3,因为你使用了input()print()方法,但是代码将在Python 2中通过改变print()方法来print 关键字 Your code would look like the following in "proper"* Python 2: 您的代码在“正确”* 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: 要在python2上使用print()语法,请将其添加到程序的顶部:

from __future__ import print_function

otherwise python will interpret the argument to print as a tuple and you'll see (). 否则python会将参数解释为print为元组,你会看到()。

I am using Python 3 and the following lines exactly give what your instructor wants: 我正在使用Python 3,以下几行准确地给出了教师想要的内容:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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