我开始学习Python,我想知道你们能否为我的问题提供一些帮助/改善我的编码。这段代码基本上是一个登录屏幕,当您输入正确的用户名和密码时,它将解锁可访问的url列表。但是,当用户输入一个大于4的值时,我很难找到一种使它“回送”的方法。
import webbrowser
import time
print("Enter Username")
name = input()
while name != 'Admin':
print("Invalid Username")
name = input()
if name == 'Admin':
print("Enter Password")
password = input()
while password != 'Password':
print("Incorrect Password")
password = input()
if password == 'Password':
print("Login Successful")
if name == 'Admin' and password == 'Password':
time.sleep(1)
print("Which Webpage would you like to visit? Select Value")
time.sleep(1)
print("0. Python Library")
print("1. ASTA Homepage")
print("2. Autotask Homepage")
print("3. Google")
print("4. Youtube")
webpage = input()
if webpage == '0':
print("Excellent Choice")
time.sleep(2)
webbrowser.open('http://www.blog.pythonlibrary.org')
if webpage == '1':
print("Excellent Choice")
time.sleep(2)
webbrowser.open('https://www.asta.com.au/')
if webpage == '2':
print("Excellent Choice")
time.sleep(2)
webbrowser.open('https://ww6.autotask.net')
if webpage == '3':
print("Excellent Choice")
time.sleep(2)
webbrowser.open('https://www.google.com.au')
if webpage == '4':
print("Excellent Choice")
time.sleep(2)
webbrowser.open('https://www.youtube.com')
答案 0 :(得分:-1)
更改以下部分:
webpage = input()
if webpage == '0':
print("Excellent Choice")
time.sleep(2)
webbrowser.open('http://www.blog.pythonlibrary.org')
if webpage == '1':
print("Excellent Choice")
time.sleep(2)
webbrowser.open('https://www.asta.com.au/')
if webpage == '2':
print("Excellent Choice")
time.sleep(2)
webbrowser.open('https://ww6.autotask.net')
if webpage == '3':
print("Excellent Choice")
time.sleep(2)
webbrowser.open('https://www.google.com.au')
if webpage == '4':
print("Excellent Choice")
time.sleep(2)
webbrowser.open('https://www.youtube.com')
收件人:
webpage = input()
while int(webpage) > 4:
webpage = input()
if webpage == '0':
print("Excellent Choice")
time.sleep(2)
webbrowser.open('http://www.blog.pythonlibrary.org')
if webpage == '1':
print("Excellent Choice")
time.sleep(2)
webbrowser.open('https://www.asta.com.au/')
if webpage == '2':
print("Excellent Choice")
time.sleep(2)
webbrowser.open('https://ww6.autotask.net')
if webpage == '3':
print("Excellent Choice")
time.sleep(2)
webbrowser.open('https://www.google.com.au')
if webpage == '4':
print("Excellent Choice")
time.sleep(2)
webbrowser.open('https://www.youtube.com')
答案 1 :(得分:-1)
这有效
webpage = input()
while not webpage.isdecimal() or int(webpage) not in range(0, 5):
print("unacceptable number.")
webpage = input()
print("Excellent Choice")
time.sleep(2)
webbrowser.open(['http://www.blog.pythonlibrary.org', 'https://www.asta.com.au/', 'https://ww6.autotask.net', 'https://www.google.com.au', 'https://www.youtube.com'][int(webpage)]