Marks Calculator - python

I keep getting syntax error highlighted on "m" in second row on the variable "mark2".
This is the code I have:
print("Please enter your 5 marks below")
mark1 = float(input("enter mark1: ")
mark2 = float(input("enter mark2: ")
mark3 = float(input("enter mark3: ")
mark4 = float(input("enter mark4: ")
mark5 = float(input("enter mark5: ")
marksList = [mark1, mark2, mark3, mark4, mark5]
print(marksList)
sumofMarks = mark1 + mark2 + mark3 + mark4 + mark5
averageOfMarks = sumofMarks/5
print("The sum of your mark is: "+str(sumofMarks))
print("The sum of your mark is: "+str(averageOfMarks))

mark1 = float(input("enter mark1: "))
^ you missed this
In the next 4 lines as well. It is better to use an IDE (Eg: PyCharm) to catch these errors.

Related

how will I put the conditionals in file handling python

so this is my code so far (I know it's not that clean forgive me):
name = input("Enter Name: ")
course = input("Enter Course: ")
section = input("Enter Section: ")
sub1 = int(input("Enter 1st Grade: "))
sub2 = int(input("Enter 2nd Grade: "))
sub3 = int(input("Enter 3rd Grade: "))
sub4 = int(input("Enter 4th Grade: "))
sub5 = int(input("Enter 5th Grade: "))
avg = int((sub1+sub2+sub3+sub4+sub5)/5)
print("Average mark:",avg)
if avg >=70:
print("Remarks: Passed")
else:
print("Remarks: Failed")
f = open("test.txt", "w+")
f.write(name)
f.write('\n')
f.write(course)
f.write('\n')
f.write(section)
f.write('\n')
f.write("The average is: ")
f.write(str(avg))
f.write('\n')
f.write("Remarks: ")
f = open("test.txt","r")
print(f.read())
f.close()
and this was supposed to be the example outcome of the txt file:
Juan deal Cruz
IT0011
A
The average is 80
Remarks: Passed
but the problem is that I don't know how to put the remarks on the f.write itself
Use another variable and carry it through to where you want it
passed = avg >= 70
passed_str = "Passed" if passed else "Failed"
print("Remarks: " + passed_str)
...
f.write("Remarks: " + passed_str)
f.write('\n')
Or use an if else like you already did for your print statement

How can I print the report without the first in the list

I'm trying to display the names except the 'name' and trying to get the average of age without 'age' on the list and lastly get the average of the grade without the 'Grade'.
I have a problem I'm trying to print the names of the list without the first row in the list(['Name', 'Age', 'Grade']) without pop or removing them from the list.
studentDB = [['Name', 'Age', 'Grade'], ['Con', 20, 90.2],
['Juan', 45, 70.2], ['Jed', 39, 100.0]]
while True:
for i in range(len(studentDB)):
print(i, studentDB[i])
print("\n********** Hackathon ***********")
CRUD = """1- Add
2- Delete
3- Edit
4- Print Report
----------------
Enter a number: """
choice = float(input(CRUD))
if (choice == 1):
Name = input("Please Enter your Name: ")
Age = int(input("Please Enter your Age: "))
Grade = float(input("Please Enter your Grade: "))
studentDB.append([Name, Age, Grade])
print(studentDB)
elif (choice == 2):
Del = int(input("Please Enter the row you want to delete: "))
studentDB.pop(Del)
elif (choice == 3):
Del1 = int(input("Please Enter the row you want to delete: "))
Name1 = input("Please Enter your Name: ")
Age1 = int(input("Please Enter your Age: "))
Grade1 = float(input("Please Enter your Grade: "))
studentDB.pop(Del1)
studentDB.insert(Del1,[Name1, Age1, Grade1])
print(studentDB)
elif (choice == 4):
#here is the problem
for s in range(1, len(studentDB)):
print("")
print(f"There are in the {s} student table. The people included in the list are.")
else:
print("Invalid input")
Here is the output that I wanted to do
This is the online compiler: https://www.programiz.com/python-programming/online-compiler/
There can be many ways to solve this. One could be to use starting index in range and use slice while calculating average of age or grades.
from statistics import mean
while True:
# Use the start index in range.
for i in range(len(studentDB)):
print(i, studentDB[i])
print("\n********** Hackathon ***********")
CRUD = """1- Add
2- Delete
3- Edit
4- Print Report
----------------
Enter a number: """
choice = float(input(CRUD))
if (choice == 1):
Name = input("Please Enter your Name: ")
Age = int(input("Please Enter your Age: "))
Grade = float(input("Please Enter your Grade: "))
studentDB.append([Name, Age, Grade])
print(studentDB)
elif (choice == 2):
Del = int(input("Please Enter the row you want to delete: "))
studentDB.pop(Del)
elif (choice == 3):
Del1 = int(input("Please Enter the row you want to delete: "))
Name1 = input("Please Enter your Name: ")
Age1 = int(input("Please Enter your Age: "))
Grade1 = float(input("Please Enter your Grade: "))
studentDB.pop(Del1)
studentDB.insert(Del1,[Name1, Age1, Grade1])
print(studentDB)
elif (choice == 4):
#here is the problem
# Use slice to get the row from index 1 afterwards
print(f"There are in the {len(studentDB) - 1} records in the student table. The people included in the list are.")
print(",".join([student[0] for student in studentDB[1:]]))
average_age = mean([row[1] for row in studentDB[1:]])
average_grades = mean([row[2] for row in studentDB[1:]])
# Print average age and grades here
else:
print("Invalid input")```

How Can I make the program rerun when the user inputs y?

print("Welcome to The pyCalc")
print("For Addition press 1")
print("For Multiplication press 2")
print("For Subtraction press 3")
print("For Division press 4")
flag = 'y'
while flag == 'y':
x = float(input("Enter your Choice(1-4): "))
if x == 1:
a = float(input("Enter 1st Value: "))
b = float(input("enter 2nd Value: "))
c = a+b
print("Sum: ", int(c))
elif x == 2:
a = float(input("Enter 1st Value: "))
b = float(input("Enter 2nd Value: "))
c = a*b
print("Product: ", int(c))
elif x == 3:
a = float(input("Enter the 1st value: "))
b = float(input("Enter the 2nd value: "))
c = a - b
print("Difference: ", int(c))
elif x == 4:
a = float(input("Enter the 1st value: "))
b = float(input("Enter the 2nd value: "))
c = a // b
print("Quotient: ", c)
else:
print("error !")
flag = print(input("Do you want to calculate more(y/n)? : ", y))
if flag == y:
continue
Hi, I'm actually new to Python so not really familiar with even basic concepts. so in this calculator program, after the first run, i want the code to rerun when the user gives an input of y. According to my knowledge, this is possible using a while loop in which we put the main code in its body and give a condition using if statement and the secondly by using the continue statement. Can somebody explain whats wrong here.
p.s. indentation may be wrong in some cases.
I have re-written your code because there were two issues:
There's no such thing as print(input(""))
flag = input("Do you want to calculate more(y/n)? : ") should be outside else
print("Welcome to The pyCalc")
print("For Addition press 1")
print("For Multiplication press 2")
print("For Subtraction press 3")
print("For Division press 4")
flag = 'y'
while flag == 'y':
x = float(input("Enter your Choice(1-4): "))
if x == 1:
a = float(input("Enter 1st Value: "))
b = float(input("enter 2nd Value: "))
c = a+b
print("Sum: ", int(c))
elif x == 2:
a = float(input("Enter 1st Value: "))
b = float(input("Enter 2nd Value: "))
c = a*b
print("Product: ", int(c))
elif x == 3:
a = float(input("Enter the 1st value: "))
b = float(input("Enter the 2nd value: "))
c = a - b
print("Difference: ", int(c))
elif x == 4:
a = float(input("Enter the 1st value: "))
b = float(input("Enter the 2nd value: "))
c = a // b
print("Quotient: ", c)
else:
print("error !")
flag = input("Do you want to calculate more(y/n)? : ")
if flag.lower() == "y":
continue

How to select just 1 element in a list for a if statement

This program prints 'Select a Valid function!' even if the input is correct, why is that? And how can I tell the program that I'm talking about individual elements in a list and not the entire list?
sign = input ("Enter the sign: ")
if sign != ["+","-","*","/"]:
print ("Select a Valid function!")
else:
print ("Let's Begin")
if sign =="+":
no1 = input("Enter the first number: ")
no2 = input("Enter the second number: ")
print (int(no1)+int(no2))
elif sign =="-":
no1 = input("Enter the first number: ")
no2 = input("Enter the second number: ")
print (int(no1)-int(no2))
elif sign =="*":
no1 = input("Enter the first number: ")
no2 = input("Enter the second number: ")
print (int(no1)*int(no2))
elif sign =="/":
no1 = input("Enter the first number: ")
no2 = input("Enter the second number: ")
print (int(no1)/int(no2))
if sign != ["+","-","*","/"]:
That will always be true, because sign is not a list, so it will never be equal to a list.
You want to use the in operator instead:
if sign not in ["+","-","*","/"]:
As others have pointed out, you're not using the correct operator to check if something is in a list.
But you don't even need that check. Just add an else: statement after all the if/elif.
sign = input ("Enter the sign: ")
if sign =="+":
no1 = input("Enter the first number: ")
no2 = input("Enter the second number: ")
print (int(no1)+int(no2))
elif sign =="-":
no1 = input("Enter the first number: ")
no2 = input("Enter the second number: ")
print (int(no1)-int(no2))
elif sign =="*":
no1 = input("Enter the first number: ")
no2 = input("Enter the second number: ")
print (int(no1)*int(no2))
elif sign =="/":
no1 = input("Enter the first number: ")
no2 = input("Enter the second number: ")
print (int(no1)/int(no2))
else:
print ("Select a Valid function!")
This way you don't need to keep the list consistent with the list of signs that you implement.
As others have said change:
if sign != ["+","-","*","/"]:
to
if sign not in ["+","-","*","/"]:
to test for non-membership of a list.
You can also cut down on a lot of duplication in your code and avoid the if...elif statements by using a dictionary of operators like this:
import operator
ops = {'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv
}
sign = input ("Enter the sign: ")
if sign not in ops:
print ("Select a Valid function!")
else:
print ("Let's Begin")
no1 = int(input("Enter the first number: "))
no2 = int(input("Enter the second number: "))
ans = ops[sign](no1, no2)
print(ans)

python "Name error: name 'a' is not defined"

I dont kow why I keep getting this issue of "Name Error: Name 'a' is not defined"
def addition():
a = input("Enter your first number: ")
b = input("Enter secondary number: ")
c = int(a) + int (b)
print(a," + ",b," = ",c," . ")
I don't know how you've indented it, but I've got it to work like this...
def addition():
a = int(input("Enter your first number: "))
b = int(input("Enter secondary number: "))
c = a + b
print(a," + ",b," = ",c," . ")
addition()

Categories

Resources