简体   繁体   English

控制结构 python 中的 While 循环使用计数器从用户那里获取输入

[英]control structures While loop in python taking input from user using a counter

I need help with a while loop.我需要 while 循环的帮助。 I cannot seem to format the code correctly in this block, so i'm writing below.我似乎无法在此块中正确格式化代码,所以我在下面写。

A user needs to enter a name with only a certain name triggering the loop.用户需要输入一个名称,只有某个名称触发循环。 Need to print out number of tries it took user before inputting correct name.在输入正确的名称之前需要打印出用户尝试的次数。

I created a user "Joe".我创建了一个用户“Joe”。 If user is Joe the code will run:如果用户是 Joe,代码将运行:

counter = 0
user = "Joe"
while name != user: 
     counter += 1
While name == user
continue

The program must count how many times it took before the name Joe is inputted.程序必须计算在输入名字 Joe 之前用了多少次。

counter = 0
user = "Joe"
name = raw_input("Enter name: ")
while name != user:
    name = raw_input("Enter name: ")
    counter += 1
print(counter)

It should be stop condition应该是停止条件

if name == user:
   print(counter) 
   //Do something here
   break

inside while loop for better flow control.内部 while 循环以获得更好的流量控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM