Python Homework (Urgent help needed!!) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Python Homework (Urgent help needed!!) (/thread-35141.html) |
Python Homework (Urgent help needed!!) - chickenseizuresalad - Oct-03-2021 Hi, I am quite new to python as I just started doing the GCSE so please don't leave any rude comments- I have probably made many mistakes. One of my homework questions was to create a program which repeatedly asks the user for a number until they enter a 10. This is the code I wrote: #Asks for a number repeatedly until you enter 10 Number= int(input("Enter a 10")) if number== 10 : print("You have entered a 10") elif Number < 10 #Number less than 10 print("Please enter a 10") input()It came out with this Could someone more experienced please tell me what went wrong? Thank u!! ps there was probably a better way to paste the code- sorry I'm new RE: Python Homework (Urgent help needed!!) - Yoriz - Oct-03-2021 See the comments added to the altered code below number = int(input("Enter a 10")) # changed Number to number if number == 10: print("You have entered a 10") elif number < 10: # changed Number to number and added : to the end # Number less than 10 print("Please enter a 10") input() RE: Python Homework (Urgent help needed!!) - snippsat - Oct-03-2021 (Oct-03-2021, 10:07 AM)chickenseizuresalad Wrote: was to create a program which repeatedly asks the user for a number until they enter a 10.Be this requirement need to use a while loop .Then the basic will like this,they to not ask for lower and higher but you can add that if task say that later. stop_number = 10 while True: number = int(input("Enter a 10: ")) if number == stop_number: print(f"{number} entered exit out") break else: print(f"Wrong number <{number}> try again") RE: Python Homework (Urgent help needed!!) - deanhystad - Oct-04-2021 I think all programming languages are case sensitive, so Number is not the same variable as number. This test is incorrect or incomplete. What do you do if the user enters 11? if number== 10 : ... elif Number < 10: ...The requirements use the word "repeatedly". Where is there any "repeatedly" in your program? You should look at loops. There are for loops and while loops in Python. Which is a good choice for this problem? The requirement says the user has to enter a number. But you are not using the input as a number. Do you need to convert the input string to an int? Is it necessary for the comparison? int(input()) is dangerous in Python. If the user enters a letter the program will crash. If the user enters a number with a decimal the program will crash. If the user presses enter without typing any characters the program will crash. If you don't plan on doing math with the input leave it as a string and compare it to '10' instead of 10. RE: Python Homework (Urgent help needed!!) - bahuiput - Oct-07-2021 (Oct-03-2021, 10:07 AM)chickenseizuresalad Wrote: Hi, I am quite new to python as I just started doing the GCSE so please don't leave any rude comments- I have probably made many mistakes. def ulit(): ten = input('Enter a 10: ') if ten == '10': print("You have entered a 10") else: ulit() ulit() RE: Python Homework (Urgent help needed!!) - buran - Oct-07-2021 (Oct-03-2021, 10:07 AM)chickenseizuresalad Wrote:#Asks for a number repeatedly until you enter 10 Number= int(input("Enter a 10")) if number== 10 : print("You have entered a 10") elif Number < 10 #Number less than 10 print("Please enter a 10") input()It came out with this Nobody commented on obvious discrepancy between the code snippet you post and the error message. In your code line #5 is elif Number < 10 while the error message indicates it is else Number < 10 . Note the use of elif vs else That means this code did not produce the error. The problem with the code that generate the error (apart from missing colon) is you can not have condition after else .
RE: Python Homework (Urgent help needed!!) - Underscore - Oct-11-2021 in the first if statement the n wasnt in capitals note: naming variables like that is consider a bad thing |