python创建提示用户输入查询条件_python流程控制练习
‘’’
if條件
‘’’
示例
sex= ‘female’
age=19
is_beautiful=True
is_successful=True
height=1.70
if sex ==‘female’ and age > 18 and age <20 and is_beautiful \
and height > 1.6 and height < 1.8 :
print(“001”)
if is_successful :
print(‘ok’)
else:
print(‘sb’)
else:
print(‘88’)
#示例2
score=int(input(‘你的成績:’))
# score=int(score)
if score >= 90:
print(“優秀”)
elif score >= 80:
print(“良好”)
elif score >= 70:
print(“普通”)
else:
print(“很差”)
while循環
tag=True
while tag:
name=input(‘your name is:’)
# pwd=input(‘your pass word is:’)
pwd=int(input(‘your pass word is:’))
if name==‘aaa’ and pwd==111:
# if name == ‘aaa’ and pwd == ‘111’:
print(‘login successful’)
else:
print(‘try again’)
print(">>>>")
while結束循環方式一:
將條件改成false
tag = True
while tag:
name = input(‘your name is:’)
# pwd=input(‘your pass word is:’)
pwd = int(input(‘your pass word is:’))
if name == ‘aaa’ and pwd == 111:
# if name == ‘aaa’ and pwd == ‘111’:
print(‘login successful’)
tag = False
else:
print(‘try again’)
print(">>>>")
while結束方式二 while+continue 結束本次循環,直接進入下一次循環
count = 1
while count < 6:
if count == 4:
count += 1
continue
print(count)
count += 1
# count += 1
‘’‘需求設計一個登陸程序
要求1、輸入正確的用戶名和密碼
要求2、登陸失敗后無限次重新輸入
要求3、登陸成功后結束程序并打印>>>,’’’
嘗試1
name = input(‘your login name:’)
pwd = input(‘your passwords :’)
tap = True
while tap :
if name == ‘bob’ and pwd ==‘111’:
print(‘login successful’)
tap = False
else:
print(‘username or passsword error’)
tap = False
print(’>>>>>’) #都打印>>>,但是只有一次登陸機會
嘗試2
while True:
name = input(‘your login name:’)
pwd = input(‘your password :’)
if name == ‘bob’ and pwd == ‘11’:
print(‘login successful’)
break
else:
print(‘user name or password error’)
break
print(’>>>’) #成功打印>>>,失敗也打印>>> 但是只有一次嘗試機會
嘗試3
while True:
name = input(‘please input your login name:’)
pwd = input(‘please input your password :’)
if name == ‘bob’ and pwd == ‘11’:
print(‘login successful’)
break
else:
print(‘user name or password error’)
print(’>>>’) #登陸成功不打印>>>,失敗打印>>> 可以無限次重新輸入
嘗試4
while True:
name = input(‘please input your login name:’)
pwd = input(‘please input your password :’)
if name == ‘bob’ and pwd == ‘11’:
print(‘login successful’,)
print(’>>>’)
break
else:
print(‘user name or password error’)
print(’>>>’)
‘’’
要求1、輸入正確的用戶名和密碼
要求2、只能嘗試3次輸入錯誤
要求3、登陸成功后結束程序
‘’’
嘗試第一次(運行結果正確,但是程序不停止,錯誤在while ture,只要更改為while count <4就可以)
count = 1
while True:
if count < 4:
name = input(‘please input your name:’)
pwd = input(‘please input your password:’)
if name == ‘egon’ and pwd == ‘11’:
print(‘login successful’)
break
else:
print(‘username or password error’)
print(’>>>’)
count += 1
count=0
while count < 3:
name=input(‘請輸入用戶名:’)
password=input(‘請輸入密碼:’)
if name == ‘egon’ and password == ‘123’:
print(‘login success’)
break
else:
print(‘用戶名或者密碼錯誤’)
count+=1
嘗試第二次 驗證while 循環中嵌套的break會導致else 不運行
count = 1
while True:
if count < 4:
name = input(‘please input your name:’)
pwd = input(‘please input your password:’)
if name == ‘egon’ and pwd == ‘11’:
print(‘login successful’)
break
else:
print(‘username or password error’)
print(’>>>’)
count += 1
else:
print(‘haha’)
嘗試第三次 驗證將while循環由tap條件終止
count = 1
tap = True
while tap:
if count < 4:
name = input(‘please input your name:’)
pwd = input(‘please input your password:’)
if name == ‘egon’ and pwd == ‘11’:
print(‘login successful’)
tap = False
else:
print(‘username or password error’)
print(’>>>’)
count += 1
else:
print(‘haha’)
‘’’
需求1、輸入正確的用戶名和密碼
要求2、只能嘗試3次輸入錯誤,有剩余次數提示
要求3、登陸成功后結束程序
‘’’
count = 3
tag=True
while tag:
if count > 0:
name = input(‘please input your name:’)
pwd = input(‘please input your password:’)
if name == “egon” and pwd == ‘11’:
print(‘login successful’)
break
else:
count -= 1
print(‘username or password error’,\
‘you can try:’,count)
if count==0:
tag=False
練習
1、使用while循環輸出1 2 3 4 5 6 8 9 10
‘’'count = 1
while True:
if count != 7:#and count < 11:
print(count)
count += 1
else:
count += 1
if count == 11:
break
#該程序雖然最終結果能按要求打印出結果,但是程序邏輯比較混亂。’’’
‘’‘教師版
count = 1
while count < 11:
if count ==7:
count +=1
continue
print(count)
count += 1
#應用continue語法,當條件成立時,停止當次循環,重新開始循環’’’
#2. 求1-100的所有數的和
count = 1
a = 0
while count < 101:
a=a +count
count += 1
print(a)
#3. 輸出 1-100 內的所有奇數
count = 1
while count < 101:
if count % 2 == 1:
print(count)
count += 1
1-100所有奇數的和
count = 1
a = 0
while count<101:
if count %2 == 1:
a = count+a
count += 1
print(a)
1~100所有偶數的和
sum = 0
n = 100
while n > 0:
sum = sum + n
n = n - 2
print(sum)
#4. 輸出 1-100 內的所有偶數
‘’‘count = 1
while count<=100:
if count %2==0:
print(count)
count += 1’’’
#5. 求1-2+3-4+5 … 99的所有數的和
count = 1
a = 0
while count < 100:
if count % 2 == 1:#99
a = a + count
count += 1
else:
a=a - count
count += 1
print(a)
1~100所有奇數的和
count=1
a=0
while count<100:
if count %2 ==1:
a=a+count
count += 1
print(a)
res=0
count=1
while count < 100:
if count%2 == 0:
res-=count
else:
res+=count
count+=1
print(res)
#6. 用戶登陸(三次機會重試)
count = 3
while True:
if count>0:
name = input(‘please input your name:’)
pwd = input(‘please input your password:’)
if name==‘bob’ and pwd==‘11’:
print(‘login successful’)
break
else:
count -= 1
print(‘username or password error’,‘you can try :’,count)
count =3
while count > 0:
name = input(‘請輸入用戶名:’)
password = input(‘請輸入密碼:’)
if name==‘bob’ and password == ‘11’:
print(‘登陸成功’)
break
else:
print(‘用戶名或密碼錯誤’)
count -= 1
‘’‘教師版
count=0
while count < 3:
name=input(‘請輸入用戶名:’)
password=input(‘請輸入密碼:’)
if name == ‘egon’ and password == ‘123’:
print(‘login success’)
break
else:
print(‘用戶名或者密碼錯誤’)
count+=1’’’
#7:猜年齡游戲
示范2
tag=True
while tag:
name = input(‘please input youe name:’).strip() #去除字符串里的空白.strip
pwd = input(‘please input your password:’)
if name==‘egon’ and pwd==‘123’:
print(‘login success’)
while tag:
print(’’’
0 退款
1 取款
2 轉賬
3 查詢
‘’’)
choice = input(‘請輸入您要執行的操作:’) #choice=‘1’
if choice==‘0’:
tag=False
elif choice==‘1’:
print(‘取款》》》’)
elif choice==‘2’:
print(‘轉賬》》’)
elif choice == ‘3’:
print(‘查詢》》》’)
else :
print(‘輸入指令錯誤,請重新輸入’)
else:
print(‘密碼錯誤’)
for i in range(1, 10):
for j
# for j in range(1, 10):
x = i * j
print(’%s * %s = %s’ % (i, j, x), end=’’)
print()
for i in range(1,10):
for j in range(1,i+1):
print(’%s*%s=%s’ %(i,j,i*j)),#end=’ ')
print()
總結
以上是生活随笔為你收集整理的python创建提示用户输入查询条件_python流程控制练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wireshark 查看端口是否正常_网
- 下一篇: java oracle sql 参数_o