使python重复一个字符串,直到输入Yes或yes

编程入门 行业动态 更新时间:2024-10-25 00:34:39
本文介绍了使python重复一个字符串,直到输入Yes或yes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

是的,我知道已经有一篇文章介绍了这一点,但是当我阅读它时,它没有帮助,所以请不要将其标记为重复.

Yes I know that there is already a post covering this, but when I read it, it didn't help so please don't mark this as a duplicate.

我想编写一个程序,询问用户是否需要建议,如果他们输入否"或否",我希望它重复问题,如果他们输入是"或是",我希望它打印建议.我希望它包含一个 while 循环

I want to write a program that asks the user if they want advice and if they input "No" or "no" I want it to repeat the question and if they input "Yes" or "yes" I want it to print the advice. I want it to include a while loop

我曾尝试自己编写它,但无法使其正常工作.

I have tried to write it myself but I can't get it to work correctly.

有人知道吗?

来自 3.4 注释的代码 -

Code from the comment for 3.4 -

def doQuestion(question, advice):
reply = ("no")

while reply  == "no":


print (question)





reply = input("Do you need some advice? ").lower()





if (reply == "yes"):


print ("Always listen to your IT teachers") 



 doQuestion("Do you want some advice?","Always listen to your IT teachers")

推荐答案

以下内容将持续检查用户是否需要有关问题的建议:

The following will keep on checking up on the user to see whether or not they need advice regarding a question:

def doTheyNeedAdvice(advice):
    while raw_input("Do you need advice? ").lower() == "no":
        pass
    print (advice)

print "How old am I?"

doTheyNeedAdvice("I am old enough")

这篇关于使python重复一个字符串,直到输入Yes或yes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 03:25:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128551.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!