The Student Room Group

Computer Science - Python

I edited this thread
(edited 3 years ago)
Original post by Spider78
Thank you so much, I really couldn't do this by myself. Sorry about the 2 - it was a typo :h:

Everything works fine but the only issue is that it keeps saying that < is not compatible with int. I have had this problem before but I have no idea how to fix it; if you are not sure as well I will try to post another question - thanks again for your help anyway! :biggrin:
(edited 3 years ago)
Original post by Spider78
Everything works fine but the only issue is that it keeps saying that < is not compatible with int. I have had this problem before but I have no idea how to fix it; if you are not sure as well I will try to post another question - thanks again for your help anyway! :biggrin:

You must convert to an int. When you get an input it is automatically string.

So every time you do an input for a number
Instead of:
num = input("blah blah blah")
Change it to:
num =int(input("blah blah blah"))

By wrapping the input in int() it casts it to an integer. We must do this as you can't check if a a string such is "10" is less than an integer value but we can check if an int such as 10 is less than an integer value.
(edited 3 years ago)
Original post by Spider78
When I did that, the program started crashing more and it kept saying that whatever numbers I entered were invalid. So this what I have coded so far. Feel free to correct me - thanks for your help!
def flightDetails():
Aircraft = int(input("Please enter the type of aircraft used: 1 = medium narrow body, 2 = large narrow body 3 = medium wide body: ")))

while Aircraft not in (1,2,3):
print("Invalid answer - Please re-enter")
Aircraft = int(input("Please enter the type of aircraft used:\n1 = medium narrow body \n2 = large narrow body 3 = medium wide body:")))

if Aircraft == 1:
print("The running cost per seat per 100km is £8 /n The maximum flight range is 2650km /n The capacity if all seats are standard class is 180 /n The minimum number of first class seats is 8")
elif Aircraft == 2:
print("The running cost per seat per 100km is £7 /n The maximum flight range is 5600km /n The capacity if all seats are standard class is 220 /n The minimum number of first class seats is 10")
elif Aircraft == 3:
print("The running cost per seat per 100km is £5 /n The maximum flight range is 4060km /n The capacity if all seats are standard class is 406 /n The minimum number of first class seats is 14")

plane_bodies = ("medium narrow body","large narrow body","medium wide body")
plane_bod_cap = [180, 220, 406]
plane_first_min = [8, 10, 14]
num = int(input("Please enter the number of first class seats on the plane: "))

if num != 0:
if num < plane_first_min[int(float(Aircraft))]:
num = input("Please enter a suitable number: ")
elif num > 0.5 * plane_bod_cap[int(float(Aircraft))]:
num = input("Please enter a suitable number: ")
else:
print("you have entered a suitable number")


Its crashing coz you changed to comparing strings with things like Aircraft == "1" instead of Aircraft==1

Technucally you can keep those that way but if you're comparing you will need to change to int.

Ive made changes in the above code in bold. Also, please don't make a variable name start with a capital letter unless its a constant, as those are mainly for classes (which you'll learn about when you do comp sci alevel).
(edited 3 years ago)
Original post by gyuigygh
Its crashing coz you changed to comparing strings with things like Aircraft == "1" instead of Aircraft==1

Technucally you can keep those that way but if you're comparing you will need to change to int.

Ive made changes in the above code in bold. Also, please don't make a variable name start with a capital letter unless its a constant, as those are mainly for classes (which you'll learn about when you do comp sci alevel).

Ignore the \n s that's i put
(edited 3 years ago)
Original post by Spider78
plane_bodies = ("medium
if num != 0:
if num < plane_first_min[int(float(Aircraft))]:
num = input("Please enter a suitable number: ")
elif num > 0.5 * plane_bod_cap[int(float(Aircraft))]:
num = input("Please enter a suitable number: ")
else:
print("you have entered a suitable number")

Also, change this part to:

plane_bodies = ("medium
if num != 0:
if num < plane_first_min[Aircraft]:
num = int(input("Please enter a suitable number: "))
elif num > 0.5 * float(plane_bod_cap[Aircraft]) :
num = int(input("Please enter a suitable number: "))
else:
print("you have entered a suitable number")
(edited 3 years ago)
Original post by gyuigygh
Ignore the \n s that's i put

Thanks so much, it works now! :smile:
Original post by Spider78
Thanks so much, it works now! :smile:

Are you sure it works properly?
If you say so.
But try entring a invalid value for the number of seats to see if allows you to enter another number and restart the process.

Also, if this is for gcse then i recommend learning about dictionaries or 2d lists as by using these it will make it easier if you wanted to add another aircraft. And it will make your code shorter.
Original post by gyuigygh
Are you sure it works properly?
If you say so.
But try entring a invalid value for the number of seats to see if allows you to enter another number and restart the process.

Also, if this is for gcse then i recommend learning about dictionaries or 2d lists as by using these it will make it easier if you wanted to add another aircraft. And it will make your code shorter.

Actually, when I enter an invalid answer it doesn't say anything and it won't restart the process - I just got rather excited that it finally said what I wanted it to when entered 0. I still need to enter an equation to it which is: capacity if all seats are standard class - number of first class seats * 2 however I can't start coding that until I get this first bit right!
Original post by Spider78
Actually, when I enter an invalid answer it doesn't say anything and it won't restart the process - I just got rather excited that it finally said what I wanted it to when entered 0. I still need to enter an equation to it which is: capacity if all seats are standard class - number of first class seats * 2 however I can't start coding that until I get this first bit right!

Can I have screenshot of code so I can have better look at indentation, but my guess is you will have to use a while loop where you get the number of seats, in case they enter wrong one.
Original post by Spider78
I attached it here

Change the last block of code to the following.
I added a while loop so it keeps asking until they get it right. I also added a line that makes them enter again in case they entered 0.
Screenshot_20200806_124529_com.sololearn.jpg
It keeps on repeating - I think that it should say it is a suitable number when 0 is entered
Original post by Spider78
It keeps on repeating - I think that it should say it is a suitable number when 0 is entered

Oh, if you want it to accept 0 as a suitable number then change the bit in the last else to set valid to True so it stops running the loop:
Screenshot_20200806_135512_com.sololearn.jpg
Original post by Spider78
Ok thanks it works now - I have one more question : how do I add the equation, capacity of standard seats - number of first class seats * 2 depending on the type of aircraft that is chosen by the user?

Equations have an equal sign. Where is yours?

Are you trying to calculate the running cost.

Do you mean:

cost = capacity of standard seats - number of first class seats * 2

If so then you will have to either make another if-else structure that checks the value of airplane and calculate the cost. Or you can restructure the program to hold the information for the airplanes in a 2d list or dictionary which is better and means less code.
(edited 3 years ago)
Original post by gyuigygh
Equations have an equal sign. Where is yours?

Are you trying to calculate the running cost.

Do you mean:

cost = capacity of standard seats - number of first class seats * 2

If so then you will have to either make another if-else structure that checks the value of airplane and calculate the cost. Or you can restructure the program to hold the information for the airplanes in a 2d list or dictionary which is better and means less code.

Yeah I did mean that sorry - I do try the if / else structure again
Original post by Spider78
Yeah I did mean that sorry - I do try the if / else structure again

I did it and my subroutine completely works - thanks again for your help!:biggrin:
Original post by Spider78
I did it and my subroutine completely works - thanks again for your help!:biggrin:

np

If you didn't want to create a new if else structure you could just use the one you already had to store the values (which would also mean you only need 1 print statemet instead of 3), then just add the equation at the end:
kidcode.png

Quick Reply

Latest