通过命令行打印 python 模数运算符

发布于 2024-07-25 22:14:00 字数 482 浏览 2 评论 0原文

我想在命令行上打印模数运算符: 例如,输出应如下所示:
1%2
2%4


30%
40%

我正在使用这样的打印语句:

print '计算 %s % %s' % (num1, 编号2)

它抛出默认错误:

类型错误:并非所有参数 在字符串格式化期间转换

现在我正在使用:

打印'计算1'+'%'+'2'

打印:

计算1%2

但请告诉我如何使用第一种方法完成此操作(:print 'computing %s % %s' % (num1,num2))

I want to print modulus operator as it is over the command line:
E.g this is how the output should look like:
1%2
2%4

or
30%
40%

I am using the print statement like this:

print 'computing %s % %s' % (num1,
num2)

Its throwing the default error:

TypeError: not all arguments
converted during string formatting

For now I am using:

print 'computing 1'+'%'+'2'

which prints:

computing 1%2

But tell me how to get this done using the first approach(:print 'computing %s % %s' % (num1,num2))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

[浮城] 2024-08-01 22:14:00

用另一个 % 符号转义 % 符号,如下所示:

print 'computing %s %% %s' % (num1, num2)

Escape the % sign with another % sign, like this:

print 'computing %s %% %s' % (num1, num2)
壹場煙雨 2024-08-01 22:14:00
print 'computing %s %% %s' % (num1, num2)
print 'computing %s %% %s' % (num1, num2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文