+ 2

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
13 Respuestas
+ 7
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
+ 5
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
+ 5
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
+ 4
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
+ 3
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
+ 2
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
+ 2
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
+ 1
You've got an indentation problem, and the input should be an integer. Here's the fix: ```python marks = int(input("marks: ")) if marks >= 90: print("A") elif marks >= 80: print("B") elif marks >= 70: print("C") else: print("D") ``` --- Basically, just make sure your `if`, `elif`, and `else` are lined up, and convert the input to a number. Should work now!
22nd Oct 2024, 10:32 PM
Melkon
Melkon - avatar
0
Your code snippet has several indentation errors and a few issues related to the input function. Here’s a corrected version of your conditional loop in Python: python Copy code # Condition loop marks = int(input("Enter marks: ")) # Convert input to integer if marks >= 90: print("A") elif marks >= 80 and marks < 90: print("B") elif marks >= 70 and marks < 80: print("C") else: print("D")
21st Oct 2024, 5:45 PM
[🌌✨𝗔𝐒𝗧ℝ𝐎✨🌌][✨𝐋𝐨𝐤𝐞𝐬𝐡𝘃𝗮𝗿𝗲𝗻💥]
[🌌✨𝗔𝐒𝗧ℝ𝐎✨🌌][✨𝐋𝐨𝐤𝐞𝐬𝐡𝘃𝗮𝗿𝗲𝗻💥] - avatar
0
Key Corrections: Indentation: Each block of code under the if, elif, and else statements must be consistently indented. Input Conversion: The input() function returns a string, so you need to convert it to an integer using int() to perform numerical comparisons. elif Alignment: The elif statements should be aligned with the if statement, not indented further.
21st Oct 2024, 5:45 PM
[🌌✨𝗔𝐒𝗧ℝ𝐎✨🌌][✨𝐋𝐨𝐤𝐞𝐬𝐡𝘃𝗮𝗿𝗲𝗻💥]
[🌌✨𝗔𝐒𝗧ℝ𝐎✨🌌][✨𝐋𝐨𝐤𝐞𝐬𝐡𝘃𝗮𝗿𝗲𝗻💥] - avatar
0
Example of Execution: If you enter a mark of 85, the output will be: Py. Output: B Make sure to maintain proper indentation when writing Python code, as it is crucial for defining code blocks.
21st Oct 2024, 5:46 PM
[🌌✨𝗔𝐒𝗧ℝ𝐎✨🌌][✨𝐋𝐨𝐤𝐞𝐬𝐡𝘃𝗮𝗿𝗲𝗻💥]
[🌌✨𝗔𝐒𝗧ℝ𝐎✨🌌][✨𝐋𝐨𝐤𝐞𝐬𝐡𝘃𝗮𝗿𝗲𝗻💥] - avatar
0
1. La première erreur c'est au niveau de la condition if. Il ne doit pas être indenté 2. Les conditions elif et else doivent s'aligner avec la condition if. 3. Seul les lignes de code après la condition doivent être indenter. Voici ma solution > #condition loop marks = int(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")
22nd Oct 2024, 10:49 AM
Diallo Mamadou Aliou
0
fixed the bugs and now it works properly missing int to convert the data type in the line 2 and fixing the indents in other lines #condition loop marks = int(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")
22nd Oct 2024, 12:42 PM
pedram mohammadi
pedram mohammadi - avatar