Hướng dẫn how do i get my python code back to a line? - làm cách nào để lấy lại mã python của tôi về một dòng?

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.

Hướng dẫn how do i get my python code back to a line? - làm cách nào để lấy lại mã python của tôi về một dòng?

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 PietersMartijn 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 <= 5:
    print 'Hello World'
    count += 1

Mỗi vòng lặp này in "Hello World" năm lần

Đã trả lời ngày 13 tháng 9 năm 2013 lúc 17:27Sep 13, 2013 at 17:27

K Dawgk DawgK DawG

12.9k9 Huy hiệu vàng33 Huy hiệu bạc65 Huy hiệu Đồng9 gold badges33 silver badges65 bronze badges

Python có các câu lệnh luồng kiểm soát thay vì các câu lệnh

#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()
6. Một triển khai của luồng điều khiển là vòng lặp
#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()
7 của Python. Bạn có thể cung cấp cho nó một điều kiện boolean (giá trị boolean là đúng hoặc sai trong python) và vòng lặp sẽ thực hiện nhiều lần cho đến khi điều kiện đó trở nên sai. Nếu bạn muốn lặp lại mãi mãi, tất cả những gì bạn phải làm là bắt đầu một vòng lặp vô hạn.

Hãy cẩn thận nếu bạn quyết định chạy mã ví dụ sau. Nhấn Control+C trong vỏ của bạn trong khi nó đang chạy nếu bạn muốn tiêu diệt quy trình. Lưu ý rằng quá trình phải ở phía trước để điều này hoạt độ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()
0

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()
8 chỉ là một bình luận. Nó không thực hiện bất cứ điều gì.
#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()
9 chỉ là một người giữ chỗ trong Python về cơ bản nói "Xin chào, tôi là một dòng mã, nhưng bỏ qua tôi vì tôi không làm gì cả."

Bây giờ hãy nói rằng bạn muốn liên tục yêu cầu người dùng nhập mãi mãi và chỉ thoát khỏi chương trình nếu người dùng nhập ký tự 'Q' để thoát.

Bạn có thể làm điều gì đó như thế 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()
1

while True:
    print('Hello world!')
0 sẽ chỉ lưu trữ bất cứ thứ gì người dùng nhập (người dùng sẽ được nhắc nhập một cái gì đó và nhấn enter). Nếu
while True:
    print('Hello world!')
0 chỉ lưu trữ chữ 'Q', mã sẽ mạnh mẽ
#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 ra khỏi vòng lặp kèm theo của nó. Tuyên bố
#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 cho phép bạn thoát khỏi bất kỳ loại vòng lặp. Ngay cả một người vô hạn! Nó cực kỳ hữu ích khi tìm hiểu nếu bạn muốn lập trình các ứng dụng người dùng thường chạy trên các vòng lặp vô hạn. Nếu người dùng không gõ chính xác chữ 'Q', người dùng sẽ được nhắc liên tục và vô hạn cho đến khi quá trình bị giết mạnh mẽ hoặc người dùng quyết định rằng anh ta có đủ chương trình khó chịu này và chỉ muốn bỏ việc.

Đã trả lời ngày 13 tháng 9 năm 2013 lúc 17:23Sep 13, 2013 at 17:23

Hướng dẫn how do i get my python code back to a line? - làm cách nào để lấy lại mã python của tôi về một dòng?

ShashankshashankShashank

13,5K5 Huy hiệu vàng36 Huy hiệu bạc61 Huy hiệu Đồng5 gold badges36 silver badges61 bronze badges

3

Viết một vòng hoặc trong khi vòng lặp và đặt tất cả mã của bạn vào bên trong nó? Lập trình loại Goto là một điều của quá khứ.

https://wiki.python.org/moin/ForLoop

Đã trả lời ngày 13 tháng 9 năm 2013 lúc 17:22Sep 13, 2013 at 17:22

A.J.A.J.A.J.

Martijn Pieters ♦ Martijn Pieters6 bronze badges

993K277 Huy hiệu vàng3912 Huy hiệu bạc3256 Huy hiệu đồng

Đã trả lời ngày 13 tháng 9 năm 2013 lúc 17:24Sep 13, 2013 at 17:24

#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()
2

Tamriver sông TamJan 12, 2015 at 13:54