Hướng dẫn while loop menu python - Python menu vòng lặp trong khi

  • If I choose 1 for the first time, and then choose 1 again for the second time,choose 1 for the first time, and then choose 1 again for the second time,
  • and then choose 2 on the second menu to return back to the first menuchoose 2 on the second menu to return back to the first menu
  • and I choose quit but it will show second menu again.choose quit but it will show second menu again.
  • I have to choose 2 for three times then the program will close.choose 2 for three times then the program will close.

It must be something wrong with my while loop but I cannot find it :[

def menu[]: # first menu
    print['+----------------------------+']
    print['|        first menu here     |']
    print['+----------------------------+']
    print['|choose [1]                  |']
    print['+----------------------------+']
    print['|choose [2] to quit          |']
    print['+----------------------------+']
    return input['>> ']

def s_menu[]: # second menu
    print['+**********************************+']
    print['|       second menu here           |']
    print['+**********************************+']
    print['|choose [1] to continue            |']
    print['+**********************************+']
    print['|choose [2] to back first menu     |']
    print['+**********************************+']
    return input['>> ']

def doSomething[]:
    print['Something here']

    second_while[]  # declare second s_menu[] here

def second_while[]: # loop second s_menu[]
    while True:
        resp = s_menu[]
        if resp == '1':
            doSomething[]
        elif resp == '2':
            menu[]
            break
        else:
            print['Wrong input!']
        #break  # if break was here, wrong user input will close terminal immediately


# loop first menu[]
while True:
    inp = menu[]
    if inp == '1':
        doSomething[]
    elif inp == '2':
        print['Break']
        break
    else:
        print['Wrong input!']
    #break  # if break was here, wrong user input will close terminal immediately

asked Aug 18, 2020 at 1:40Aug 18, 2020 at 1:40

Change

def doSomething[]:
    print['Something here']

    second_while[]  # declare second s_menu[] here

to

def doSomething[]:
    print['Something here']

Otherwise every time you call

[[1,2,3],[4,5,6],[7,8,9]]
3, you go 1 layer of
[[1,2,3],[4,5,6],[7,8,9]]
4 deeper, and will need to press 2 one more time to exit all of them.2 one more time to exit all of them.

answered Aug 18, 2020 at 1:47Aug 18, 2020 at 1:47

JasmijnJasmijnJasmijn

8,4802 gold badges27 silver badges42 bronze badges2 gold badges27 silver badges42 bronze badges

2

ok, I have found the solution for my answer. While loop inside the while loop

# loop first menu[]
while True:
    menu[]
    inp = input['>> ']
    if inp == '1':
        doSomething[]
        # loop doSomething[]
        while True:
            resp = input['>> ']
            if resp == '1':
                doSomething[]
            elif resp == '2':
                break
            else:
                print['Wrong input']
    elif inp == '2':
        print['Break']
        break
    else:
        print['Wrong input!']

answered Aug 18, 2020 at 2:44Aug 18, 2020 at 2:44

Vì vậy, tôi đang nghĩ đến việc tạo ra một mảng 2D trong đó hàng đại diện cho tên thành phố [tất nhiên là trong số nguyên] và cột đại diện cho nhiệt độ.

Nội dung chính ShowShow

  • 1. Danh sách lồng nhau: Xử lý và in ấn
  • 2. Danh sách lồng nhau: Tạo
  • 3. Làm thế nào để bạn nhập một mảng hai chiều?
  • 3. Làm thế nào để bạn nhập một mảng hai chiều?
  • 3. Làm thế nào để bạn nhập một mảng hai chiều?
  • Làm thế nào để bạn lặp lại một mảng 2D trong Python?
  • Làm thế nào để bạn lặp lại một mảng trong Python?
  • Làm thế nào để bạn thêm vào danh sách 2D trong Python?
  • Làm thế nào để bạn tạo ra một mảng 2 chiều numpy?

days = int[input["Enter days: "]]
city = int[input["Enter city: "]]
array = [[0]* days]*city
i = 0

Tạo một mảng hai chiều Nếu bạn chỉ sử dụng hàm Arange, nó sẽ xuất ra một mảng một chiều.Để biến nó thành một mảng hai chiều, chuỗi đầu ra của nó với chức năng định hình lại.Đầu tiên, 20 số nguyên sẽ được tạo và sau đó nó sẽ chuyển đổi mảng thành một mảng hai chiều với 4 hàng và 5 cột.chain its output with the reshape function. First, 20 integers will be created and then it will convert the array into a two-dimensional array with 4 rows and 5 columns.

days = 3
city = 3

Vì vậy, tôi đang nghĩ đến việc tạo ra một mảng 2D trong đó hàng đại diện cho tên thành phố [tất nhiên là trong số nguyên] và cột đại diện cho nhiệt độ.

[[0,0,0],[0,0,0],[0,0,0]]

Nội dung chính Show

1. Danh sách lồng nhau: Xử lý và in ấn

while i

Bài Viết Liên Quan

Chủ Đề