关于Python中的ifelse、while和for循环的简单小结

1.ifelse

1.1首先简单编辑一个关于ifelse的程序:
_username = ‘yanfeixu’
_password = ‘*fan’
username = input(“username:”)
password = input(“password:”)
print(username,password)

if _username == username and _password == password:
print(“Welcome user {name} login…”.format(name=username))
else:
print(“Invalid username or password!”)

# getpass可以使密码变成密文,但是在pycharm中不方便使用
#username = input(“username:”)
#password = getpass.getpass(“password:”)
#print(username,password)

关于Python中的ifelse、while和for循环的简单小结

1.2.当设计到三个选项的时候,就要利用到elif
if guess_age == age_of_oldboy:
print(“yes,you got it.”)
elif guess_age > age_of_oldboy:
print(“think smaller…”)
else:
print(“think bigger!”)
关于Python中的ifelse、while和for循环的简单小结

2.while

age_of_oldboy = 56
count = 0

while True: # 可以利用循环,不会只执行一次就退出 # if count == 3: # 执行三次就退出 # break

while count < 3: # 和上面的三行注释是一样的效果
guess_age = int(input(“guess age:”))
if guess_age == age_of_oldboy:
print(“yes,you got it.”)
break
elif guess_age > age_of_oldboy:
print(“think smaller…”)
else:
print(“think bigger!”)
count += 1
else:
print(“you have tries too much times…”)
关于Python中的ifelse、while和for循环的简单小结
关于Python中的ifelse、while和for循环的简单小结

3.for

age_of_oldboy = 56
for i in range(3): # 循环的第二种表示方法for
guess_age = int(input(“guess age:”))
if guess_age == age_of_oldboy:
print(“yes,you got it.”)
break
elif guess_age > age_of_oldboy:
print(“think smaller…”)
else:
print(“think bigger!”)

else:
print(“you have tries too much times…”)
关于Python中的ifelse、while和for循环的简单小结

关于Python中的ifelse、while和for循环的简单小结
关于Python中的ifelse、while和for循环的简单小结
关于Python中的ifelse、while和for循环的简单小结