0

I am trying to do a true/false check in an if/else nested statement and I am not sure how to go about it.

marks = float(input("Enter marks: "))

if marks >70:
    if work_submitted == True:
        grade = 'A'
    else:
        grade = 'B'
elif marks >=60 and marks <70:
    if work_submitted == True:
        grade = 'B'
    else:
        grade = 'C'
elif marks >=50 and marks <60:
    if work_submitted == True:
        grade = 'C'
    else:
        grade = 'F'
else:
    if work_submitted == True:
        grade = 'F'
    else:
        grade = 'F'

print("your grade is ", grade)

When trying to debug the code, it states that work_submitted is not defined. Hence, I also tried adding:

work_submitted = input("Did you submit your work?(True/False): ") 

after

marks = float(input...) 

but it does not work.

Also, from limited knowledge, it is not possible to define an input as a boolean. Is there any way to resolve this issue?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Spoons
  • 1
  • 1
  • Whatever you entered through `input` is string; it never equals booleans. – Austin Apr 15 '20 at 16:09
  • Help us by adding the `work_submitted` line to you code and posting the full traceback error message from python. Lets focus on this one thing. – tdelaney Apr 15 '20 at 16:11
  • The colon on the end of that line looks suspicious, I want to see the real code in action – tdelaney Apr 15 '20 at 16:11
  • 1
    An `if` statement is not a loop, so the `nested-loops` tag is not appropriate. Also, the problem has *nothing to do with* your if statements; it's clear that what you're really asking is "how do I read user input and get a boolean value as a result?" The question I linked will provide useful discussion for you to solve this problem. – Karl Knechtel Apr 15 '20 at 16:17

2 Answers2

0

You can give boolean as 0 or 1 in input and convert it into int. sample code->

marks = float(input("Enter marks: "))
work_submitted = int(input("Did you submit your work?(True/False): "))
if work_submitted==True:
    print('yes')
else:
    print('no')
Puneet
  • 1
  • 1
0

Convenient solution for the user:

work_submitted = input("Did you submit your work?(yes/no): ")
work_submitted = work_submitted.lower().startswith("y")

With lower() you can make sure it doesn't matter if the user writes caps or not.
The startswith("y") checks if the string starts with a "y" enabling the user just to type "y" (or "n") as is common in many cli's.

ricffb
  • 21
  • 4