-3

Exam Question

This is my answer. I'm struggling to type proper code for this particular code. I keep getting syntax errors. I'm not very good at coding I don't know where I have made mistakes.

name = input("enter your name, ")
surname = input("enter your surname, ")
electric = input("electric amount used in kwh is, ")
discount_contract = input( " Do you have a discount contract? ")


electric_amount = float(input("Electric amount is: "))

AU = 0.91555 
VAT = 0.18
DU = 0.2651

A = AU * electric_amount
D = DU * electric_amount
F = A * 0.007

total_before_VAT = A + D + F
total_electric_bill = (1 + VAT ) * total_before_VAT


if discount_contract == "yes" :
    VAT2 = VAT - D
    print("Dear" , name , surname, )
    print("You used" , VAT2 and "you have a contract")

if discount_contract == "no" : 
      print("Dear" , name , surname, )
      print("You used" , electric_amount and "your total electric bill is" : total_electric_bill)


  while discount_contract != "yes" or "no" :
    input("do you have a discount contract? ")
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 3
    *what* syntax errors do you get? What does the error message show? – 2e0byo Nov 12 '21 at 17:33
  • 1
    `print("You used" , VAT2 and "you have a contract")` Did you mean `print("You used" , VAT2, "and you have a contract")` ? I recommend f-strings: `print(f"You used {VAT2} and you have a contract")` – 001 Nov 12 '21 at 17:35
  • shouldn't `print("You used" , VAT2 and "you have a contract")` be `print("you used", Vat2, "and you have a contract")? Here you test the logical condition 'vat2 and "you have a contract"', which is likely *not* what you want to test (the string is truthy) – 2e0byo Nov 12 '21 at 17:35
  • 2
    Are you taking the exam right now? If so, isn't it prohibited to use this site? – j1-lee Nov 12 '21 at 17:35
  • Also at `while discount_contract != "yes" or "no" :`. See [How to test multiple variables against a single value?](https://stackoverflow.com/a/15112149) – 001 Nov 12 '21 at 17:35
  • check out [flake8](https://pypi.org/project/flake8/), static linters are just fantastic for helping catch trivial errors! – ti7 Nov 12 '21 at 17:36
  • also your indentation is all over the place, and there's a floating ':' in one of the prints. get your head round where something is a string (e.g. to print to the user) or a command to python itself (i.e. python syntax) – 2e0byo Nov 12 '21 at 17:37
  • im not at exam. i just want to know where did i wrong – NaNo Blin Nov 12 '21 at 17:38
  • 2
    @j1-lee's comment is a very important point. We don't mind helping with homework questions which is what I assumed this was, *providing* you [ask them properly](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions). Incidentally I post that link a hundred times a week atm. If this really is an assessed exam, you really play by the rules. In the long run it just works out better for everyone. – 2e0byo Nov 12 '21 at 17:39
  • Ah yes ı understand by which i mean as saying exam question i meant this is previous exam question. i m trying to solve it to make practice my exam. sorry for the misunderstood – NaNo Blin Nov 12 '21 at 17:43

1 Answers1

0

Problems

  • You should be using format specifiers, ie
variable = 'cake'
print(f'{variable} is good')
# output - cake is good
  • The while loop should be right below discount_contract variable, to prevent the code from executing further if the options are not yes or no
name = input("enter your name, ")
surname = input("enter your surname, ")
electric = input("electric amount used in kwh is, ")
discount_contract = input("do you have a discount contract? ")

while discount_contract.lower() not in ('yes', 'no') :
    discount_contract = input("do you have a discount contract? ")


electric_amount = float(input("Electric amount is: "))

AU = 0.91555
VAT = 0.18
DU = 0.2651

A = AU * electric_amount
D = DU * electric_amount
F = A * 0.007

total_before_VAT = A + D + F
total_electric_bill = (1 + VAT) * total_before_VAT

print(f"Dear {name} , {surname}\n")
if discount_contract == "yes":
    VAT2 = VAT - D
    print(f"You used {VAT2} and you have a contract")
else:
    print(f"You used {electric_amount} and your total electric bill is {total_electric_bill}")
AnanthDev
  • 1,605
  • 1
  • 4
  • 14