Python Forum
Python Homework (Urgent help needed!!)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Homework (Urgent help needed!!)
#1
Exclamation 
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

Error:
File "main.py", line 5 else Number < 10 ^ SyntaxError: invalid syntax

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
Yoriz write Oct-03-2021, 10:29 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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()
Reply
#3
(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") 
Yoriz write Oct-03-2021, 10:47 AM:
Homework question, give the op a chance to carry on trying to solve this their self now the current error has been rectified.
Reply
#4
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.
Reply
#5
(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.
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

Error:
File "main.py", line 5 else Number < 10 ^ SyntaxError: invalid syntax

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

def ulit():
    ten = input('Enter a 10: ')
    if ten == '10': print("You have entered a 10")
    else: ulit()
ulit()
buran write Oct-07-2021, 05:04 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#6
(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

Error:
File "main.py", line 5 else Number < 10 ^ SyntaxError: invalid syntax

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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
in the first if statement the n wasnt in capitals
note:
naming variables like that is consider a bad thing

666+666*666/666-666
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HELP in python homework makashito 4 3,895 Oct-12-2021, 10:12 AM
Last Post: buran
  CyperSecurity Using Python HomeWork ward1995 1 1,948 Jul-08-2021, 03:55 PM
Last Post: buran
Exclamation urgent , Python homework alm 2 2,284 May-09-2021, 11:19 AM
Last Post: Yoriz
  urgent I got a syntax errors alm 2 5,832 Feb-28-2021, 02:54 PM
Last Post: alm
Heart Urgent homework help needed Medou 4 2,688 Nov-24-2020, 09:28 AM
Last Post: buran
  Homework with python Johnsonmfw 1 1,676 Sep-20-2020, 04:03 AM
Last Post: ndc85430
  [Urgent] build code GodMaster 2 1,790 Mar-23-2020, 12:25 AM
Last Post: jefsummers
  Bifid Genkey (Urgent) Laura123 2 2,034 Mar-09-2020, 08:09 PM
Last Post: micseydel
  Python Homework Help *Urgent GS31 2 2,564 Nov-24-2019, 01:41 PM
Last Post: ichabod801
  Python Homework Question OrcDroid123 1 2,362 Sep-01-2019, 08:44 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020