+ 1

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
7 odpowiedzi
+ 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
+ 2
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
+ 1
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
+ 1
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
Per Bratthammar , Thank you so much for your valuable suggestions. This is a remarkable thing that now I know because of you!
20th Oct 2024, 11:03 PM
Prakash Kumar Jaiswal
Prakash Kumar Jaiswal - avatar
0
Prakash, Sorry i missed your other errors. I was just responding to you inquiry about indentation errors. I was on my phone and didn't try to run the code. I just corrected the indentation. The good news is that the Amazing Lothar and others came to the rescue with other tips to help you get your code running. Python is actually a really flexible language. It's just picky about indentation. With a tiny bit of practice, it will be second nature and you'll be writing Python scripts with ease.
21st Oct 2024, 12:22 AM
Jerry Hobby
Jerry Hobby - avatar