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

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

问题描述

我绝对是python的初学者.我编写了一个程序来检查一个数是否为素数.但它给了我上述类型错误

该错误的含义是什么,我该如何解决?

我看到了相同标题的问题.但我不明白如何解决它.所以我问这个问题.

num = ( "哪个不.你想检查素数:" )我 = 1k = 0while(i <= num):if(num % i == 0): #idle 在这里显示类型错误k=k+1我=我+1如果(k == 2):打印 "%d 是质数" % num别的:打印 "%d 不是质数" % num

解决方案

num is string.

<预><代码>>>>num = (哪个不.你想检查素数与否:")>>>数量 % 1回溯(最近一次调用最后一次):文件<stdin>",第 1 行,位于 <module>类型错误:并非所有参数都在字符串格式化期间转换>>>

我想你错过了raw_input():

<预><代码>>>>num = int(raw_input("哪个不.你想检查素数:"))没有.你想检查素数与否:1>>>数量1

I am an absolute beginner of python. I had coded a program for checking whether a number is prime or not. but it is giving me the above type error

What is the meaning of that error and how should I resolve it?

I saw the questions with same title. but I didn't understand how to resolve it. so I am asking this question.

num = ( "which no. u want to check prime or not:" )
i = 1
k = 0
while(i <= num):
  if(num % i == 0): #idle is showing type error here 
       k=k+1
       i=i+1
  if(k == 2):
       print "%d is prime number" % num
  else:
       print "%d is not a prime no" % num

解决方案

num is string.

>>> num = ( "which no. u want to check prime or not:" )
>>> num % 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
>>>

I think you missed raw_input():

>>> num = int(raw_input( "which no. u want to check prime or not:" ))
which no. u want to check prime or not:1
>>> num
1

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

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