Daksh Angaraj
Daksh Angaraj

Reputation: 59

Two list are getting printed

when i try to the below code i get the list printed in not expected order. I want to have a nested list of hotels and availabe rooms(with ac,tv,) Please see the expected output and help me out.

name=str(input("name of your hotel?"))
n=int(input("Number of rooms?" ))
room=[]
if(n>0):

    room_tmp=[name]
    for i in range (0,n):
        room_tmp.append(i)
        ac=str(input("Air conditioning available?in room "))
        if(ac=="y" or"Y"):
            room_tmp.append("Air Conditioning")

        else:
            room_tmp.append("No Air Conditioning")
            fb=str(input("Free Breakfast Available? in room",))
            if(fb=="y" or "Y" ):
                room_tmp.append("Free Breakfast")
            else:
                room_tmp.append("Free Breakfast Not Available")
            tv=str(input("Televison Acailabe?"))
            if(tv=="y" or "Y"):
                room_tmp.append("TV Available")
            else:
                room_tmp.append("No TV")
            wifi=str(input("Free WiFi Available?"))
            if(wifi=="y" or "Y"):
                room_tmp.append("Wifi")
            else: 
                room_tmp.append("No Wifi")
            budget=int(input("Enter the cost?"))
            room_tmp.append(budget)

            room.append(room_tmp)
            number=number+1
    else:
        print("room cannot be 0")
    print(room)

Given output-

[['asa', 0, 'Air Conditioning', 'Free Breakfast', 'TV Available', 'Wifi', 9, 1, 'Air Conditioning', 'Free Breakfast', 'TV Available', 'Wifi', 8], 
 ['asa', 0, 'Air Conditioning', 'Free Breakfast', 'TV Available', 'Wifi', 9, 1, 'Air Conditioning', 'Free Breakfast', 'TV Available', 'Wifi', 8]]

Expected output I want this to get printed and not the above.

[['asa', 0, 'Air Conditioning', 'Free Breakfast', 'TV Available', 'Wifi', 9]
 [ "asa',1, 'Air Conditioning', 'Free Breakfast', 'TV Available', 'Wifi', 8]]

Upvotes: 0

Views: 64

Answers (1)

Gustav Rasmussen
Gustav Rasmussen

Reputation: 3971

The temporary list did not get reset between iterations of the for loop before, but now the following is added at the final step in the loop:

del room_tmp:

I simplified the code a bit: it now re-iterates on room-number error and is additionally prepared to fail early if number of rooms is not greater than zero, with

sys.exit

which is provided in a comment (if desired, remember to also import the built-in sys module).

And the if-else blocks has been replaced by ternary operators for readability:

from pprint import pprint as pp
# import sys

name = str(input("name of your hotel?"))
n = int(input("Number of rooms?" ))

if not (n > 0):
    # sys.exit("Number of rooms must be positive integer")
    n = int(input("Number of rooms must be positive integer, try again:" ))

rooms = []

for i in range(n):
    room_tmp = [name, i]

    ac = str(input("Air conditioning available in room?"))
    room_tmp.append("Air Conditioning") if (ac.upper() == "Y") else room_tmp.append("No Air Conditioning")

    fb = str(input("Free Breakfast Available in room?"))
    room_tmp.append("Free Breakfast") if (fb.upper() == "Y") else room_tmp.append("Free Breakfast Not Available")

    tv = str(input("Televison Availabe?"))
    room_tmp.append("TV Available") if (tv.upper() == "Y") else room_tmp.append("No TV")

    wifi = str(input("Free WiFi Available?"))
    room_tmp.append("Wifi") if (wifi.upper() == "Y") else room_tmp.append("No Wifi")

    room_tmp.append(int(input("Enter the cost?")))

    rooms.append(room_tmp)
    del room_tmp

pp(rooms)

Sample output:

[['Four_seasons', 0, 'Air Conditioning', 'Free Breakfast', 'TV Available', 'Wifi', 5],
 ['Four_seasons', 1, 'Air Conditioning', 'Free Breakfast', 'TV Available', 'Wifi', 6]]

Upvotes: 1

Related Questions