Python:在字符串格式化期间,并非所有参数都已转换 [英] Python: not all arguments converted during string formatting

查看:60
本文介绍了Python:在字符串格式化期间,并非所有参数都已转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码给出了错误

print('type a whole number:')
n = input()
if n % 2 == 1:
    print('Odd');
else:
    print('Even');

我假设我必须对if语句中的变量n做些特殊的事情?我是Python的初学者.

I'm assuming there's something special I have to do to variable n in the if statement? I am a beginner to Python.

推荐答案

以下是解决方法:

n = int(input("type a whole number:"))

由于 input()返回一个字符串,因此需要转换首先使用 int()将其转换为int.

Since input() returns a string, you need to convert it to an int first, using int().

这篇关于Python:在字符串格式化期间,并非所有参数都已转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆