我想,循环问题 [英] problems with looping, i suppose

查看:58
本文介绍了我想,循环问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在做的一项练习,用于猜测1-100的数字。只是为了

有趣(哈哈)我决定添加一些错误检查,现在当我运行

时,DOS提示快速闪烁并消失。起初,我只有顶级的尝试/除了块而不是第二个,并且它起作用了

(尽管它可以工作),但是在添加之后第二次尝试/除外,它

有这个问题。我确定它与循环有关,并且

休息/继续。


我会感激任何帮助,以及对程序的任何建议

让你逐步调试代码(我已经习惯了Visual Studio)。

我在家安装了SPE,但没有在这里工作,所以我现在不能尝试它。
。我假设它有一个调试器。


谢谢。

Here''s an exercise I was doing to guess a number from 1-100. Just for
fun (ha ha) I decided to add some error checking too, and now when I run
it, the DOS prompt flashes real quick and disappears. At first, I had
just the top try/except block and not the second one, and that worked
(as far as it would work), but after adding the second try/except, it
has this problem. I''m sure it has something to do with the looping and
the breaks/continues.

I''d appreciate any help on this, and also any suggestions for programs
that let you step through code to debug it (I''m used to Visual Studio).
I have SPE installed at home, but not here at work so I can''t try it
right now. I assume it has a debugger.

Thanks.

推荐答案

John Salerno写道:
John Salerno wrote:
这是我正在做的一个练习
Here''s an exercise I was doing




这可能会有所帮助:


导入随机

number = random.choice(range(1,100))

tries = 0

而True:

尝试:

guess =输入(''输入介于1和100之间的数字:'')

break

除了NameError:

print''无效的数字\ n''

继续


而True:

尝试+ = 1

尝试:

如果猜= =数字:

打印''恭喜!你得到它'',尝试','猜测。''

休息

elif guess<数字:

guess =输入('太低。再试一次:'')

elif guess>数字:

guess =输入('太高。再试一次:'')

除了NameError:

print''无效的数字\\ n''

继续


raw_input()



This might help:

import random

number = random.choice(range(1, 100))
tries = 0

while True:
try:
guess = input(''Enter a number between 1 and 100: '')
break
except NameError:
print ''Invalid number\n''
continue

while True:
tries += 1
try:
if guess == number:
print ''Congratulations! You got it in'', tries, ''guess(es).''
break
elif guess < number:
guess = input(''Too low. Try again: '')
elif guess > number:
guess = input(''Too high. Try again: '')
except NameError:
print ''Invalid number\n''
continue

raw_input()


John Salerno写道:
John Salerno wrote:
这是一个我想做的练习,从1-100开始猜测一个数字。
Here''s an exercise I was doing to guess a number from 1-100.




这是另一个相关的问题:<正确的时候是


年= raw_input(''输入年份(或其他退出的角色):'')


尝试:

年= int(年)

除了NameError:

break


如果(年%4 == 0)和(年%100!= 0或年%400 == 0):

打印年份,''是闰年。\ n''

else:

打印年份,''不是闰年。\ n''


raw_input()


这是按预期工作的,除非你输入任何其他字符

而不是数字,程序刚退出。为什么它还没有执行

raw_input函数并暂停?我认为这也是

其他练习中发生的事情,但我不知道为什么。如果你突破循环,

它是否仍然会在raw_input暂停?



Here''s another question that is related:

while True:
year = raw_input(''Enter year (or other character to quit): '')

try:
year = int(year)
except NameError:
break

if (year % 4 == 0) and (year % 100 != 0 or year % 400 == 0):
print year, ''is a leap year.\n''
else:
print year, ''is not a leap year.\n''

raw_input()

This works as expected, except that if you enter any character other
than a number, the program just quits. Why doesn''t it still execute the
raw_input function and pause? I think this is what is happening in the
other exercise too, but I don''t know why. If you break out of the loop,
should it still pause at raw_input?


只是看看代码答案:

检查第二个试块的范围。


如果那不起作用......

我会读它真实的:)


尝试使用eclipse进行PyDev插件 - 这对我很有帮助,但是我确实来自Java - 所以我是一个日食爱好者已经。

Just barely looked the code answer:
check you scope on the second try block.

if that doesn''t work...
I''ll read it for real :)

Try PyDev plugin with eclipse - it''s served me fairly well, but I did
come from Java - so I''m an eclipse fan already.


这篇关于我想,循环问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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