python如何进入下一行_如何在python3的循环中进入下一行?

1586010002-jmsa.png

I have made a little "game where you guess the number. However I have a problem. I need to be able to restart the loop instead of having the "wrong/right answer" text be repeated.

Here is what I have:number = 34

guess = int(input("guess the number"))

while guess != 34:

if guess > number:

print("Too high")

else:

print("too low")

if guess == number:

print("You got it.")

解决方案You only take input once. You''ll need to update guess (get new input from the user) at the end of your loop if it''s not the correct number.

Pretend you''re playing the game with a friend you''re the "computer" and your friend is the user who has to guess the number you''re thinking of. How will that go?

1) Your freind makes a guess

2) You evaluate that guess, to low/high or correct

3) You repeat 1) and 2) untill the guess is correct

So you''ll now you should be able to realize that you need to take a guess and evaluate it in a loop.

First of all, can''t you see that the algorithm itself is incorrect?

The code will bring the "else" branch of your "if" when the "guess" is not more than 34, so if it is exactly 34, you will print "too low". This is not correct, as in next line you will print "You got it". Not mentioning that you''re using immediate constant of 34 hard-coded in your function. Is the goal of the game to guess 34? :-) Hard-coded constants should always be avoided, except for widely used constants like 0 or 1.

As to your question, you apparently don''t know what the loop is, so it''s hard to guess what you mean by "next line". There is nothing which requires a fragment of code to be repeated. Just read the manual: http://docs.python.org/tutorial/controlflow.html[^].

—SA

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值