Tôi đang cố gắng tìm ra cách làm cho Python quay trở lại đầu mã. Trong Smallbasic, bạn làm
start:
textwindow.writeline["Poo"]
goto start
Nhưng tôi không thể tìm ra cách bạn làm điều đó trong Python:/ bất kỳ ý tưởng nào?
Mã tôi đang cố gắng lặp là cái này
#Alan's Toolkit for conversions
def start[] :
print ["Welcome to the converter toolkit made by Alan."]
op = input ["Please input what operation you wish to perform. 1 for Fahrenheit to Celsius, 2 for meters to centimetres and 3 for megabytes to gigabytes"]
if op == "1":
f1 = input ["Please enter your fahrenheit temperature: "]
f1 = int[f1]
a1 = [f1 - 32] / 1.8
a1 = str[a1]
print [a1+" celsius"]
elif op == "2":
m1 = input ["Please input your the amount of meters you wish to convert: "]
m1 = int[m1]
m2 = [m1 * 100]
m2 = str[m2]
print [m2+" m"]
if op == "3":
mb1 = input ["Please input the amount of megabytes you want to convert"]
mb1 = int[mb1]
mb2 = [mb1 / 1024]
mb3 = [mb2 / 1024]
mb3 = str[mb3]
print [mb3+" GB"]
else:
print ["Sorry, that was an invalid command!"]
start[]
Vì vậy, về cơ bản, khi người dùng hoàn thành việc chuyển đổi của họ, tôi muốn nó quay trở lại đầu. Tôi vẫn không thể đặt các ví dụ vòng lặp của bạn vào thực tế với điều này, vì mỗi lần tôi sử dụng hàm def để lặp, nó nói rằng "OP" không được xác định.
CodeForester
36.1k16 Huy hiệu vàng104 Huy hiệu bạc128 Huy hiệu đồng16 gold badges104 silver badges128 bronze badges
Hỏi ngày 13 tháng 9 năm 2013 lúc 17:20Sep 13, 2013 at 17:20
0
Sử dụng một vòng lặp vô hạn:
while True:
print['Hello world!']
Điều này chắc chắn cũng có thể áp dụng cho chức năng
#Alan's Toolkit for conversions
def start[] :
print ["Welcome to the converter toolkit made by Alan."]
op = input ["Please input what operation you wish to perform. 1 for Fahrenheit to Celsius, 2 for meters to centimetres and 3 for megabytes to gigabytes"]
if op == "1":
f1 = input ["Please enter your fahrenheit temperature: "]
f1 = int[f1]
a1 = [f1 - 32] / 1.8
a1 = str[a1]
print [a1+" celsius"]
elif op == "2":
m1 = input ["Please input your the amount of meters you wish to convert: "]
m1 = int[m1]
m2 = [m1 * 100]
m2 = str[m2]
print [m2+" m"]
if op == "3":
mb1 = input ["Please input the amount of megabytes you want to convert"]
mb1 = int[mb1]
mb2 = [mb1 / 1024]
mb3 = [mb2 / 1024]
mb3 = str[mb3]
print [mb3+" GB"]
else:
print ["Sorry, that was an invalid command!"]
start[]
3 của bạn; Bạn có thể thoát khỏi vòng lặp bằng #Alan's Toolkit for conversions
def start[] :
print ["Welcome to the converter toolkit made by Alan."]
op = input ["Please input what operation you wish to perform. 1 for Fahrenheit to Celsius, 2 for meters to centimetres and 3 for megabytes to gigabytes"]
if op == "1":
f1 = input ["Please enter your fahrenheit temperature: "]
f1 = int[f1]
a1 = [f1 - 32] / 1.8
a1 = str[a1]
print [a1+" celsius"]
elif op == "2":
m1 = input ["Please input your the amount of meters you wish to convert: "]
m1 = int[m1]
m2 = [m1 * 100]
m2 = str[m2]
print [m2+" m"]
if op == "3":
mb1 = input ["Please input the amount of megabytes you want to convert"]
mb1 = int[mb1]
mb2 = [mb1 / 1024]
mb3 = [mb2 / 1024]
mb3 = str[mb3]
print [mb3+" GB"]
else:
print ["Sorry, that was an invalid command!"]
start[]
4 hoặc sử dụng #Alan's Toolkit for conversions
def start[] :
print ["Welcome to the converter toolkit made by Alan."]
op = input ["Please input what operation you wish to perform. 1 for Fahrenheit to Celsius, 2 for meters to centimetres and 3 for megabytes to gigabytes"]
if op == "1":
f1 = input ["Please enter your fahrenheit temperature: "]
f1 = int[f1]
a1 = [f1 - 32] / 1.8
a1 = str[a1]
print [a1+" celsius"]
elif op == "2":
m1 = input ["Please input your the amount of meters you wish to convert: "]
m1 = int[m1]
m2 = [m1 * 100]
m2 = str[m2]
print [m2+" m"]
if op == "3":
mb1 = input ["Please input the amount of megabytes you want to convert"]
mb1 = int[mb1]
mb2 = [mb1 / 1024]
mb3 = [mb2 / 1024]
mb3 = str[mb3]
print [mb3+" GB"]
else:
print ["Sorry, that was an invalid command!"]
start[]
5 để thoát hoàn toàn chức năng, điều này cũng chấm dứt vòng lặp:def start[]:
print ["Welcome to the converter toolkit made by Alan."]
while True:
op = input ["Please input what operation you wish to perform. 1 for Fahrenheit to Celsius, 2 for meters to centimetres and 3 for megabytes to gigabytes"]
if op == "1":
f1 = input ["Please enter your fahrenheit temperature: "]
f1 = int[f1]
a1 = [f1 - 32] / 1.8
a1 = str[a1]
print [a1+" celsius"]
elif op == "2":
m1 = input ["Please input your the amount of meters you wish to convert: "]
m1 = int[m1]
m2 = [m1 * 100]
m2 = str[m2]
print [m2+" m"]
if op == "3":
mb1 = input ["Please input the amount of megabytes you want to convert"]
mb1 = int[mb1]
mb2 = [mb1 / 1024]
mb3 = [mb2 / 1024]
mb3 = str[mb3]
print [mb3+" GB"]
else:
print ["Sorry, that was an invalid command!"]
Nếu bạn cũng có thể thêm một tùy chọn để bỏ thuốc, đó có thể là:
if op.lower[] in {'q', 'quit', 'e', 'exit'}:
print["Goodbye!"]
return
Ví dụ.
Đã trả lời ngày 13 tháng 9 năm 2013 lúc 17:22Sep 13, 2013 at 17:22
Martijn Pieters ♦ Martijn Pieters♦Martijn Pieters
993K277 Huy hiệu vàng3912 Huy hiệu bạc3256 Huy hiệu đồng277 gold badges3912 silver badges3256 bronze badges
3
Python, giống như hầu hết các ngôn ngữ lập trình hiện đại, không hỗ trợ "goto". Thay vào đó, bạn phải sử dụng các chức năng điều khiển. Về cơ bản có hai cách để làm điều này.
1. Vòng lặp
Một ví dụ về cách bạn có thể làm chính xác những gì ví dụ nhỏ của bạn làm như sau:
while True :
print "Poo"
Nó đơn giản mà.
2. đệ quy
def the_func[] :
print "Poo"
the_func[]
the_func[]
Lưu ý về đệ quy: Chỉ làm điều này nếu bạn có một số lần cụ thể bạn muốn quay lại từ đầu [trong trường hợp đó thêm một trường hợp khi đệ quy nên dừng lại]. Đó là một ý tưởng tồi để thực hiện một đệ quy vô hạn như tôi xác định ở trên, bởi vì cuối cùng bạn sẽ hết bộ nhớ!
Được chỉnh sửa để trả lời câu hỏi cụ thể hơn
#Alan's Toolkit for conversions
invalid_input = True
def start[] :
print ["Welcome to the converter toolkit made by Alan."]
op = input ["Please input what operation you wish to perform. 1 for Fahrenheit to Celsius, 2 for meters to centimetres and 3 for megabytes to gigabytes"]
if op == "1":
#stuff
invalid_input = False # Set to False because input was valid
elif op == "2":
#stuff
invalid_input = False # Set to False because input was valid
elif op == "3": # you still have this as "if"; I would recommend keeping it as elif
#stuff
invalid_input = False # Set to False because input was valid
else:
print ["Sorry, that was an invalid command!"]
while invalid_input: # this will loop until invalid_input is set to be False
start[]
Đã trả lời ngày 13 tháng 9 năm 2013 lúc 17:24Sep 13, 2013 at 17:24
Tamriver sông TamRiver Tam
2.9363 Huy hiệu vàng31 Huy hiệu bạc 50 Huy hiệu Đồng3 gold badges31 silver badges50 bronze badges
14
Bạn có thể dễ dàng làm điều đó với các vòng lặp, có hai loại vòng lặp
Cho các vòng lặp: Loops:
for i in range[0,5]:
print 'Hello World'
Trong khi vòng lặp: Loops:
count = 1
while count