Can anyone see the flaw in my code?

selfteaching

couple issues with my code I need help working out.

  1. how do I get the header to only print once above the results, and not at all if nothing meetings the criteria?
  2. the else component of my code is no longer working, it used to just print if none of the other scenarios were true but I updated it and now it keeps printing every time. Can anyone spot the issue?

Code:

import Hotels

htl_1= Hotels.Hotels(111,100,1,1)
htl_2= Hotels.Hotels(112,200,2,1)
htl_3= Hotels.Hotels(113,250,2,2)
htl_4= Hotels.Hotels(114,300,3,2)
htl_5= Hotels.Hotels(115,350,3,3)


feeInput=input('Enter maximum per night fee: ')
roomInput=input('Enter minimum number of bedrooms: ')
bathInput=input('Enter minimum number of baths: ')


header='{:10} {:10} {:10} {:10}'.format('Room#','Cost','Rooms','Bathrooms')

print(header)

if int(feeInput)>= htl_1.fee and int(roomInput)<= htl_1.rooms and int(bathInput)<= htl_1.bath:
    print(htl_1.gethtl())
if int(feeInput)>= htl_2.fee and int(roomInput)<= htl_2.rooms and int(bathInput)<= htl_2.bath:
    print(htl_2.gethtl())
if int(feeInput)>= htl_3.fee and int(roomInput)<= htl_3.rooms and int(bathInput)<= htl_3.bath:
    print(htl_3.gethtl())
if int(feeInput)>= htl_4.fee and int(roomInput)<= htl_4.rooms and int(bathInput)<= htl_4.bath:
    print(htl_4.gethtl())
if int(feeInput)>= htl_5.fee and int(roomInput)<= htl_5.rooms and int(bathInput)<= htl_5.bath:
    print(htl_5.gethtl())

else:
    print('Sorry, no rooms available that meet that criteria')
Zak Stucke

Without knowing the Hotels module I believe this syntax is correct. In order for the else to only activate when none of those if statements are true, you need to change them to elif (after the first if statement) as shown. The .format you used only works inside of a print statment as shown. Thanks

import Hotels

htl_1= Hotels.Hotels(111,100,1,1)
htl_2= Hotels.Hotels(112,200,2,1)
htl_3= Hotels.Hotels(113,250,2,2) 
htl_4= Hotels.Hotels(114,300,3,2)
htl_5= Hotels.Hotels(115,350,3,3)

feeInput = int(input('Enter maximum per night fee: '))
roomInput = int(input('Enter minimum number of bedrooms: '))
bathInput = int(input('Enter minimum number of baths: '))

header = True

if feeInput >= htl_1.fee and roomInput <= htl_1.rooms and bathInput<= htl_1.bath: 
    print(htl_1.gethtl())
elif feeInput >= htl_2.fee and roomInput <= htl_2.rooms and bathInput <= htl_2.bath: 
    print(htl_2.gethtl()) 
elif feeInput >= htl_3.fee and roomInput <= htl_3.rooms and bathInput <= htl_3.bath: 
    print(htl_3.gethtl()) 
elif feeInput >= htl_4.fee and roomInput <= htl_4.rooms and bathInput <= htl_4.bath: 
    print(htl_4.gethtl()) 
elif feeInput >= htl_5.fee and roomInput <= htl_5.rooms and bathInput <= htl_5.bath: 
    print(htl_5.gethtl())
else: 
    print('Sorry, no rooms available that meet that criteria')
    header = False

if header == True:
    print('{0} {1} {2} {3}'.format(RoomVar,CostVar,RoomsVar,BathroomsVar))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SmartContracts/Solidity: Can anyone see my smartcontract Code? Or is it kept private?

My vigenere cypher code cannot decrypt. Can anyone see what is the problem in my code?

Write a convolution code, but returns me a "Mosaic" image. Can anyone see what is wrong with my code?

"TypeError: Can't convert 'list' object to str implicitly" Can anyone see what will fix my code?

Can anyone see the issue with my code (cannot convert int to boolean) Processing

Can anyone notice where the mistakes is in my code?

Can anyone tell me if they see an error in the following PHP code?

Discord - Can anyone other than admin see the code inside bot?

Can anyone tell me, why my js code is not running on jsfiddle?

can anyone tell what's the bug in my quick sort code

Can anyone help me find what is wrong with my code?

Can anyone tell me how to increase the snake size in my code?

Can anyone point out what mistake I made in my code?

Can anyone tell me what is wrong in my code

why is my code not giving the proper output?Can anyone provide a hint?

Can't get div to wrap around my header. Can anyone see why?

Can anyone correct this code?

Can anyone explain this code

Can anyone see why my valid-looking SQL fails when run using SQLCMD?

Can anyone see mistake in this javascript code? Filling 2D array

WebRTC: Sound not muting - can anyone see an error?

Can anyone see a fault in the logic of these two algorithms?

Failing hidden CodeFights test - can't find flaw in code

Hello. Can anyone tell my why is my code Section='"+lblSection.Text+"' paramater doesnt works

all my data changed when i update only 1 data .Can anyone help to check my code?

In this code i can't run operation to insert database in laravel , may anyone askme where is wrong in my code

Can anyone explain the output of this code?

Anyone can comment this ruby code?

Git push - Who can see the code of my project?