Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner code help
#1
Hi everyone, I am new to Python and would appreciate some greatly needed help. I am using functions with csv files and my code doesn't seem to work, please can you help:

x = 0
score = x
import csv

datafile = open('questions.csv', 'r')
myreader = csv.reader(datafile)

print ("Welcome to the Quiz, please choose a quiz number. \n 1.Sports \n 2.Geography")
choice = input()

if choice == "1":
Sportsmenu()

if choice == "2":
print ("Geography")

def Sportsmenu():
print ("Welcome to the Sports quiz, please choose a difficulty level. \n 1.Easy \n 2.Medium \n 1.Hard")
choice = input()

if choice == "1":
def easysports():
if choice == "2":
print ("Medium")

def easysports():
datafile = open('questions.csv', 'r')
myreader = csv.reader(datafile)
print("Welcome to Easy Sports")
for row in myreader:
print(row[0])
answer1 = input("Please enter your answer ")

if answer1.lower() == (row[2]):
print("Correct")
x = x + 1
else:
print("Incorrect")

print(row[1])
answer2 = input("Please enter your answer ")
if answer2.lower() == (row[3]):
print("Correct")
x = x + 1
else:
print("Incorrect")


score = (x / 2) * 100
name = input("What is your name? ")
print(name, x, "out of 2, your score is", score,)

writefile = open("scores.csv","a")
writefile.write(name + "," + score+ "\n")
writefile.close()
print("Score added to file")

Thanks
Reply
#2
Please put your code in Python code tags and errors in error tags, you can find help here.
If you get no error but code does not behave as you want, describe both, desired and actual outcome (you can use output tags).
Reply
#3
Traceback (most recent call last):
File "//12144domain.sch/userdata$/users/staff/miahn/Desktop/Works/test.py", line 12, in <module>
Sportsmenu()
NameError: name 'Sportsmenu' is not defined
Reply
#4
Sorry for repost

I am new to Python and would appreciate some greatly needed help. I am using functions with csv files and my code doesn't seem to work, please can you help:

x = 0
score = x
import csv

datafile = open('questions.csv', 'r')
myreader = csv.reader(datafile)

print ("Welcome to the Quiz, please choose a quiz number. \n 1.Sports \n 2.Geography")
choice = input()
    
if choice == "1":
    Sportsmenu()

if choice == "2":
            print ("Geography")

def Sportsmenu():
    print ("Welcome to the Sports quiz, please choose a difficulty level. \n 1.Easy \n 2.Medium \n 1.Hard")
choice = input()
    
if choice == "1":
        def easysports():
            if choice == "2":
                print ("Medium")

def easysports():
    datafile = open('questions.csv', 'r')
myreader = csv.reader(datafile)
print("Welcome to Easy Sports")
for row in myreader:
    print(row[0])
    answer1 = input("Please enter your answer ")

if answer1.lower() == (row[2]):  
    print("Correct")
    x = x + 1   
else:
    print("Incorrect")

print(row[1])
answer2 = input("Please enter your answer ")
if answer2.lower() == (row[3]):       
    print("Correct")
    x = x + 1   
else:
    print("Incorrect")


score = (x / 2) * 100
name = input("What is your name? ")
print(name, x, "out of 2, your score is", score,)

writefile = open("scores.csv","a")
writefile.write(name + "," + score+ "\n")
writefile.close()
print("Score added to file")
Error:
Traceback (most recent call last):
  File "//12144domain.sch/userdata$/users/staff/miahn/Desktop/Works/test.py", line 13, in <module>
    Sportsmenu()
NameError: name 'Sportsmenu' is not defined
Reply
#5
Hi, Im new to Python and would appreciate some help. The code below doesnt work but I cant work out why.

x = 0
score = x
import csv

datafile = open('questions.csv', 'r')
myreader = csv.reader(datafile)

print ("Welcome to the Quiz, please choose a quiz number. \n 1.Sports \n 2.Geography")
choice = input()
    
if choice == "1":
    Sportsmenu()

if choice == "2":
            print ("Geography")

def Sportsmenu():
    print ("Welcome to the Sports quiz, please choose a difficulty level. \n 1.Easy \n 2.Medium \n 1.Hard")
choice = input()
    
if choice == "1":
        def easysports():
            if choice == "2":
                print ("Medium")

def easysports():
    datafile = open('questions.csv', 'r')
myreader = csv.reader(datafile)
print("Welcome to Easy Sports")
for row in myreader:
    print(row[0])
    answer1 = input("Please enter your answer ")

if answer1.lower() == (row[2]):  
    print("Correct")
    x = x + 1   
else:
    print("Incorrect")

print(row[1])
answer2 = input("Please enter your answer ")
if answer2.lower() == (row[3]):       
    print("Correct")
    x = x + 1   
else:
    print("Incorrect")


score = (x / 2) * 100
name = input("What is your name? ")
print(name, x, "out of 2, your score is", score,)

writefile = open("scores.csv","a")
writefile.write(name + "," + score+ "\n")
writefile.close()
print("Score added to file")
Error
Traceback (most recent call last):
File "F:\test monday 19th.py", line 13, in <module>
Sportsmenu()
NameError: name 'Sportsmenu' is not defined
Reply
#6
Definition of Sportsmenu function is after (line 17) it is first called (line 12). As with variables being assigned, functions need to be defined before they are used, so just change the order of your code appropriately.
Reply
#7
You're trying to call a function before you've defined it. Move the definition before the call, and it will work.

[EDIT] After answering the question, I noticed a duplicate, and merged the threads. It's a little messy but I prefer not deleting anything.
Reply
#8
I understand what you guys are saying but its a menu system, hence the selections come further down the program. E.g. a user selects option one then it carried out a function.
Reply
#9
From what you're saying, it sounds like you believe that defining a function will result in its execution. This isn't true. You can define functions and never call them, which means they will not be executed. Please give a try to what we're saying, and if it still doesn't work, please describe the expected vs. actual output.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner: Code not work when longer list raiviscoding 2 765 May-19-2023, 11:19 AM
Last Post: deanhystad
  Code not reading http link from .txt file (Beginner level) plarrip 3 2,353 Dec-17-2020, 11:33 PM
Last Post: bowlofred
  Beginner: I need help understanding few lines of a code. hop_090 1 1,642 Sep-07-2020, 04:02 PM
Last Post: Larz60+
  Beginner Code, how to print something after a number of turns (guessing game) QTPi 4 2,682 Jun-18-2020, 04:59 PM
Last Post: QTPi
  A beginner code... TheDude 7 3,217 Jun-18-2020, 05:39 AM
Last Post: TheDude
  [Beginner] Code is not producing desired result fakej171 2 2,380 Mar-21-2020, 10:26 AM
Last Post: buran
  what function should i use to tidy up my code (extreme beginner) scraig0117 4 2,255 Dec-16-2019, 04:03 PM
Last Post: scraig0117
  Beginner at Python. Trying to count certain integer from random string of code kiaspelleditwrong 3 2,367 Oct-14-2019, 10:40 AM
Last Post: perfringo
  Beginner trying to code in python RA0211 1 1,816 Sep-26-2019, 11:10 AM
Last Post: emryscass
  Beginner Code bh3282 9 4,463 Mar-18-2019, 03:58 PM
Last Post: samsonite

Forum Jump:

User Panel Messages

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