0

Condition loop in python

Check indentation error: #condition loop marks = input("marks: ") if(marks >= 90): print("A") elif(marks >= 80 and marks < 90): print("B") elif(marks >= 70 and marks < 80): print("C") else: print("D")

20th Oct 2024, 4:27 PM
Prakash Kumar Jaiswal
Prakash Kumar Jaiswal - avatar
5 Antworten
+ 4
The if statement should not be indented . The elif should align with the if. The print statements should be indented. #condition loop marks = input("marks: ") if(marks >= 90): print("A") elif(marks >= 80 and marks < 90): print("B") elif(marks >= 70 and marks < 80): print("C") else: print("D")
20th Oct 2024, 5:00 PM
Jerry Hobby
Jerry Hobby - avatar
+ 4
Prakash Kumar Jaiswal , > i am not sure how far you have progressed with the 'introduction to python' tutorial, but many of the issues you are encountering are covered in that tutorial." so i would recommend to continue with this course. please also practice what you have learned. >> another issue is the data type of the input, which is a string, and how it is used in the conditional statements. this causes a `TypeError`. to avoid this, we need to convert the input values to integers.
20th Oct 2024, 6:17 PM
Lothar
Lothar - avatar
0
Jerry Hobby , Thanks a lot sir !😊👍 But there is data type problem which might be solved by converting the input to int(input ()).
20th Oct 2024, 7:05 PM
Prakash Kumar Jaiswal
Prakash Kumar Jaiswal - avatar
0
Lothar , Thanks for your valuable advice, Mr Lothar, I am counting on you 🙏😊
20th Oct 2024, 7:07 PM
Prakash Kumar Jaiswal
Prakash Kumar Jaiswal - avatar
0
Hi Prakash Kumar Jaiswal, In Python, it’s generally not necessary to use parentheses in the condition of compound statements, like this: elif(marks >= 70 and marks < 80): ... Instead, you can write it without parentheses, like this: elif marks >= 70 and marks < 80: ... If you still want to use parentheses for clarity, you can write it like this, but it’s optional: elif (marks >= 70) and (marks < 80): ... Also, you can simplify the expression by using a more Pythonic approach, which makes the code easier to read: elif 70 <= marks < 80: ... This method takes advantage of Python’s ability to chain comparisons, which can make your code cleaner and more concise.
20th Oct 2024, 10:08 PM
Per Bratthammar
Per Bratthammar - avatar