Python:raw_input 读取数字的问题 [英] Python: Problem with raw_input reading a number

查看:75
本文介绍了Python:raw_input 读取数字的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是 raw_input 没有做我需要它做的事情.我想要做的是得到 totPrimes = 我在提示符下输入的任何内容.如果我在计数 时替换 totPrimeswhile count <50 这个脚本有效.如果我在提示中输入 50,这个脚本不起作用,恐怕 raw_input 不是我想要使用的函数吗?这是我的代码片段:

unfortunately raw_input is not doing what I need it to do. What I am trying to do is get totPrimes = whatever I type in at the prompt. If i replace while count < totPrimes with while count < 50 this script works. If I type 50 into the prompt, this script doesnt work, I'm afraid raw_input isn't the function im looking to use? Here is a snippet of my code:

testNum = 3
div = 2
count = 1
totPrimes = raw_input("Please enter the primes: ")

while count < totPrimes :
    while div <= testNum :

推荐答案

totPrimes = int(totPrimes)
while count < totPrimes:
    # code

raw_input 为您提供一个字符串,您必须在进行任何数字比较之前将其转换为整数或浮点数.

raw_input gives you a string you must convert to an integer or float before making any numeric comparison.

这篇关于Python:raw_input 读取数字的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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