如何在Python中创建循环 [英] How to create a loop in Python

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

问题描述

这是我的代码:

my_Sentence = input('Enter your sentence. ')
sen_length = len(my_Sentence)
sen_len = int(sen_length)
while not (sen_len < 10 ):
  if sen_len < 10:
      print ('Good')
  else:
      print ('Wo thats to long')
  break

我正在尝试使程序不断要求用户写一个句子,直到它少于10个字符.我需要知道如何再次使用该程序作为一个句子,但是我认为最简单的方法是使代码从头开始.但我不急于做到这一点.有人可以帮忙吗?

解决方案

模式

反复提示用户输入的一般模式是:

# 1. Many valid responses, terminating when an invalid one is provided
while True:
    user_response = get_user_input()
    if test_that(user_response) is valid:
        do_work_with(user_response)
    else:
        handle_invalid_response()
        break

我们使用无限循环while True:,而不是重复两次我们的get_user_input函数(帽尖). /p>

如果要检查相反的情况,只需更改break的位置:

# 2. Many invalid responses, terminating when a valid one is provided
while True:
    user_response = get_user_input()
    if test_that(user_response) is valid:
        do_work_with(user_response)
        break
    else:
        handle_invalid_response()

如果您需要循环工作,但在用户提供无效输入时向用户发出警告,则只需添加一个测试,即可检查某种quit命令,并且仅在其中进行break检查:

# 3. Handle both valid and invalid responses
while True:
    user_response = get_user_input()

    if test_that(user_response) is quit:
        break

    if test_that(user_response) is valid:
        do_work_with(user_response)
    else:
        warn_user_about_invalid_response()

将模式匹配到您的具体情况

您要提示用户向您提供少于10个字符的句子.这是模式2的实例(许多无效响应,仅需要一个有效响应).将模式2映射到您的代码上,我们得到:

# Get user response
while True:
    sentence = input("Please provide a sentence")
    # Check for invalid states
    if len(sentence) >= 10:
        # Warn the user of the invalid state
        print("Sentence must be under 10 characters, please try again")
    else:
        # Do the one-off work you need to do
        print("Thank you for being succinct!")
        break

This is my code:

my_Sentence = input('Enter your sentence. ')
sen_length = len(my_Sentence)
sen_len = int(sen_length)
while not (sen_len < 10 ):
  if sen_len < 10:
      print ('Good')
  else:
      print ('Wo thats to long')
  break

I'm trying to make the program ask the user continuously to write a sentence, until it is under 10 characters. I need to know how to have the program as for a sentence again, but I think the simplest way would be to have the code start from the top; but I'm not surte how to do that. Can someone help?

解决方案

The pattern

The general pattern for repeatedly prompting for user input is:

# 1. Many valid responses, terminating when an invalid one is provided
while True:
    user_response = get_user_input()
    if test_that(user_response) is valid:
        do_work_with(user_response)
    else:
        handle_invalid_response()
        break

We use the infinite loop while True: rather than repeating our get_user_input function twice (hat tip).

If you want to check the opposite case, you simply change the location of the break:

# 2. Many invalid responses, terminating when a valid one is provided
while True:
    user_response = get_user_input()
    if test_that(user_response) is valid:
        do_work_with(user_response)
        break
    else:
        handle_invalid_response()

If you need to do work in a loop but warn the user when they provide invalid input then you just need to add a test that checks for a quit command of some kind and only break there:

# 3. Handle both valid and invalid responses
while True:
    user_response = get_user_input()

    if test_that(user_response) is quit:
        break

    if test_that(user_response) is valid:
        do_work_with(user_response)
    else:
        warn_user_about_invalid_response()

Mapping the pattern to your specific case

You want to prompt a user to provide you a less-than-ten-character sentence. This is an instance of pattern #2 (many invalid responses, only one valid response required). Mapping pattern #2 onto your code we get:

# Get user response
while True:
    sentence = input("Please provide a sentence")
    # Check for invalid states
    if len(sentence) >= 10:
        # Warn the user of the invalid state
        print("Sentence must be under 10 characters, please try again")
    else:
        # Do the one-off work you need to do
        print("Thank you for being succinct!")
        break

这篇关于如何在Python中创建循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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