Home > OS >  Why does my code keep printing the first data in the array?
Why does my code keep printing the first data in the array?

Time:02-07

i = 0
one_adult = [20.00,30.00]
one_child = [12.00,18.00]
one_senior = [16.00,24.00]
family_ticket = [60.00,90.00]
groups_of_six = [15.00,22.50]
lion_feeding = 2.50
penguin_feeding = 2.00
evening_barbecue = 5.00


Ticket_Type = ["one_adult",one_adult[i],"one_child",one_child[i],"one_senior",one_senior[i],"family_ticket",family_ticket[i],"groups_of_six",groups_of_six[i]]


Attractions_1day = ["lion_feeding",lion_feeding,"penguin_feeding",penguin_feeding]
Attractions_2day = ["lion_feeding",lion_feeding,"penguin_feeding",penguin_feeding,"evening_barbecue",evening_barbecue]


option = input("are you buying tickets for one day? (yes or no)")
if option == "yes" :
  print()
  print("These are ticket types available for one day tickets and their prices $")
  print()
  print(Ticket_Type)
  print()
  print("These are the attractions available for one day tickets and their prices $")
  print()
  print(Attractions_1day)
elif option == "no" :
  i = i   1
  print()
  print("These are ticket types available for two day tickets and their prices $")
  print()
  print(Ticket_Type)
  print()
  print("These are the attractions available for two day tickets and their prices $")
  print()
  print(Attractions_2day)

Why does it keep printing the data when i = 0 and not the data when i = 1? It is supposed to print one_adult, 30 not one_adult 20 etc The program is supposed to display the options available for people to buy tickets (1 day / 2 day)

CodePudding user response:

You're trying to increment i in the elif; however, that does not affect the elements of Ticket_Type.

The fastest way to resolve this is to reassign the Ticket_Type list in the elif statement:

i = 0
one_adult = [20.00,30.00]
one_child = [12.00,18.00]
one_senior = [16.00,24.00]
family_ticket = [60.00,90.00]
groups_of_six = [15.00,22.50]
lion_feeding = 2.50
penguin_feeding = 2.00
evening_barbecue = 5.00


Ticket_Type = ["one_adult",one_adult[i],"one_child",one_child[i],"one_senior",one_senior[i],"family_ticket",family_ticket[i],"groups_of_six",groups_of_six[i]]


Attractions_1day = ["lion_feeding",lion_feeding,"penguin_feeding",penguin_feeding]
Attractions_2day = ["lion_feeding",lion_feeding,"penguin_feeding",penguin_feeding,"evening_barbecue",evening_barbecue]


option = input("are you buying tickets for one day? (yes or no)")
if option == "yes" :
  print()
  print("These are ticket types available for one day tickets and their prices $")
  print()
  print(Ticket_Type)
  print()
  print("These are the attractions available for one day tickets and their prices $")
  print()
  print(Attractions_1day)
elif option == "no" :
  i = i   1
  Ticket_Type = ["one_adult",one_adult[i],"one_child",one_child[i],"one_senior",one_senior[i],"family_ticket",family_ticket[i],"groups_of_six",groups_of_six[i]]
  print()
  print("These are ticket types available for two day tickets and their prices $")
  print()
  print(Ticket_Type)
  print()
  print("These are the attractions available for two day tickets and their prices $")
  print()
  print(Attractions_2day)

CodePudding user response:

Move the line:

Ticket_Type = ["one_adult",one_adult[i],"one_child",one_child[i],"one_senior",one_senior[i],"family_ticket",family_ticket[i],"groups_of_six",groups_of_six[i]]

to if and elif clause

if option == "yes" :
//Here
  Ticket_Type = ["one_adult",one_adult[i],"one_child",one_child[i],"one_senior",one_senior[i],"family_ticket",family_ticket[i],"groups_of_six",groups_of_six[i]]
  print()
  print("These are ticket types available for one day tickets and their prices $")
  print()
  print(Ticket_Type)
  print()
  print("These are the attractions available for one day tickets and their prices $")
  print()
  print(Attractions_1day)
elif option == "no" :
  i = i   1
//And here.
  Ticket_Type = ["one_adult",one_adult[i],"one_child",one_child[i],"one_senior",one_senior[i],"family_ticket",family_ticket[i],"groups_of_six",groups_of_six[i]]
  print()
  print("These are ticket types available for two day tickets and their prices $")
  print()
  print(Ticket_Type)
  print()
  print("These are the attractions available for two day tickets and their prices $")
  print()
  print(Attractions_2day)

CodePudding user response:

Check your code properly, You have specified i=0 and and in every place in the Ticket_type array you have put one_adult[i] which is just the first element of the array i.e "20". You haven't increased the value of i after every iteration. like you have to put loops to increase the value so that other elements of the array is printed.

To solve your problem do this

["one_adult",one_adult[0],"one_child",one_child[1],"one_senior",one_senior[2],"family_ticket",family_ticket[3],"groups_of_six",groups_of_six[4]]

Reply if this was helpful or you want better explanation.

  •  Tags:  
  • Related