Tôi đã tạo một menu lựa chọn yêu cầu bạn nhập lệnh bạn muốn chạy, tôi đã thực hiện điều này bằng cách sử dụng các câu lệnh hỏi lệnh nào để chạy. Đây là mã tôi có vào lúc này [hiện tại tôi không có gì làm điều này]:
# Asks which command you want to run. word = input["Which command? "] # Runs the command/s. if word == "info": print["Info command."] elif word == "replace": print["Replace command."] elif word == "ping": print["Ping command"] else: print["Command not found"]
Sẽ thật tuyệt vời nếu ai đó có thể giúp đỡ, cảm ơn.
Hỏi ngày 28 tháng 12 năm 2012 lúc 18:37Dec 28, 2012 at 18:37
1
Xin lỗi nếu điều này là quá nhiều, nhưng bạn có thể muốn xem xét đưa nó vào một chức năng để làm điều gì đó như thế này:
def main[]:
# Asks which command you want to run.
word = input["Which command? "].strip[].lower[]
# Runs the command/s.
if word == "info":
print["Info command."]
elif word == "replace":
print["Replace command."]
elif word == "ping":
print["Ping command"]
elif word == "quit":
return False
else:
print["Command not found"]
return True
while main[]:
pass
Đây sẽ giống như while True
sau đó
def main[]:
# Asks which command you want to run.
word = input["Which command? "].strip[].lower[]
# Runs the command/s.
if word == "info":
print["Info command."]
elif word == "replace":
print["Replace command."]
elif word == "ping":
print["Ping command"]
elif word == "quit":
return False
else:
print["Command not found"]
return True
while main[]:
pass
0Đã trả lời ngày 28 tháng 12 năm 2012 lúc 18:44Dec 28, 2012 at 18:44
jackcogdilljackcogdilljackcogdill
4.7623 huy hiệu vàng27 Huy hiệu bạc47 Huy hiệu đồng3 gold badges27 silver badges47 bronze badges
0
Hãy thử gói mã của bạn trong một khối
def main[]:
# Asks which command you want to run.
word = input["Which command? "].strip[].lower[]
# Runs the command/s.
if word == "info":
print["Info command."]
elif word == "replace":
print["Replace command."]
elif word == "ping":
print["Ping command"]
elif word == "quit":
return False
else:
print["Command not found"]
return True
while main[]:
pass
1. Điều này sẽ lặp lại mã vô thời hạn.Để đọc thêm, hãy thử điều này
Đã trả lời ngày 28 tháng 12 năm 2012 lúc 18:40Dec 28, 2012 at 18:40
1
Câu trả lời được đề xuất
Lời khuyên của Wooeee là tại chỗ, theo như nó đi. Tuy nhiên, nếu bạn đang cố gắng lặp lại một cái gì đó mà không biết phải lặp lại bao nhiêu lần, những gì bạn muốn là một vòng lặp
2.def main[]: # Asks which command you want to run. word = input["Which command? "].strip[].lower[] # Runs the command/s. if word == "info": print["Info command."] elif word == "replace": print["Replace command."] elif word == "ping": print["Ping command"] elif word == "quit": return False else: print["Command not found"] return True while main[]: pass
pagechoice = '1' while pagechoice != '0': pagechoice = input["Please choose one:"] …
Nhảy để đăng
Tất cả 4 câu trả lời
woooee814 gần một maven đăng 814 Nearly a Posting Maven Sử dụng chức năng //www.tutorialspoint.com/python/python_funces.htmm và gọi nó nhiều lần như bạn muốnimport time
def print_something[ctr]:
choices = ["zero", "one", "two", "three", "four",
"five", "six"]
if ctr < len[choices]:
print[" This is page", choices[ctr]]
else:
print["Sorry, but what you have typed in is invalid,\nPlease try again."]
ctr=1
print_something[ctr]
time.sleep[1.0]
ctr = 6
print_something[ctr]
time.sleep[1.0]
ctr = 10
print_something[ctr]
Được chỉnh sửa 8 năm trước bởi Woooee
Schol-R-LEA1,446 Commie Mutant Faitor có poster nổi bật 1,446 Commie Mutant Traitor Featured Poster
8 năm trướcLời khuyên của Wooeee là tại chỗ, theo như nó đi. Tuy nhiên, nếu bạn đang cố gắng lặp lại một cái gì đó mà không biết phải lặp lại bao nhiêu lần, những gì bạn muốn là một vòng lặp
def main[]:
# Asks which command you want to run.
word = input["Which command? "].strip[].lower[]
# Runs the command/s.
if word == "info":
print["Info command."]
elif word == "replace":
print["Replace command."]
elif word == "ping":
print["Ping command"]
elif word == "quit":
return False
else:
print["Command not found"]
return True
while main[]:
pass
2.pagechoice = '1'
while pagechoice != '0':
pagechoice = input["Please choose one:"]
#Now it will determine which page you choose.
if pagechoice == '1':
print[""" This is page one """]
elif pagechoice == '2':
print[""" This is page two """]
elif pagechoice == '3':
print[""" This is page three """]
elif pagechoice == '4':
print[""" This is page four """]
elif pagechoice == '5':
print[""" This is page five """]
elif pagechoice == '6':
print[""" This is page six """]
else:
print["Sorry, but what you have typed in is invalid,\nPlease try again."]
Nhảy để đăng
for x in ['a', 'd', 'm', 'e', 'h']:
print[x]
Tất cả 4 câu trả lời
woooee814 gần một maven đăng
8 năm trước
Sử dụng chức năng //www.tutorialspoint.com/python/python_funces.htmm và gọi nó nhiều lần như bạn muốn
i.e Call that function and have 1 correct 'yes' responce and an else 'no responce'.
Được chỉnh sửa 8 năm trước bởi Woooee
Schol-R-LEA1,446 Commie Mutant Faitor có poster nổi bật 1,446 Commie Mutant Traitor Featured Poster
8 năm trướcSử dụng chức năng //www.tutorialspoint.com/python/python_funces.htmm và gọi nó nhiều lần như bạn muốn
def print_something[ctr]:
if ctr == '1':
print["This is a valid entry."]
else:
print["Sorry, but what you have typed in is invalid,\nPlease try again."]
Được chỉnh sửa 8 năm trước bởi Woooee
woooee814 gần một maven đăng
8 năm trước
Sử dụng chức năng //www.tutorialspoint.com/python/python_funces.htmm và gọi nó nhiều lần như bạn muốn
Được chỉnh sửa 8 năm trước bởi Woooee