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
Related
This progam was working fine before i used data validation(the while loops) but now i get the following error -
average_grade = (grade1 + grade2) / 2
NameError: name 'grade2' is not defined
This error was not happening before , I have just started programming so i am quite confused
valid = False
while valid==False:
grade1 = float( input("Enter the grade1: ") )
if grade1 <0 or grade1 > 10:
print('Please enter a grade between 0 and 10')
continue
else:
valid = True
while valid==False:
grade2 = float(input("Enter the grade2: "))
if grade2 < 0 or grade2 > 10:
print('Please enter a grade between 0 and 10')
continue
else:
valid = True
while valid == False:
absences = int(input("Enter the number of absences: "))
if type(absences)!=int:
print("Number of absences can only be an integer value")
continue
else:
valid = True
while valid == False:
total_classes = int(input("Enter the total number of classes: "))
if type(total_classes)!=int:
print("Total number of classes can only be an integer value")
continue
else:
valid = True
average_grade = (grade1 + grade2) / 2
attendance = ((total_classes - absences) / total_classes)*100
print(average_grade)
print("Your attendance is :", attendance , "%")
if(average_grade>=6):
if(attendance>=0.8):
print("YOU HAVE PASSED")
else:
print("You have failed as your attendance is less than 80%")
elif(attendance>=0.8):
print("You have failed due to average grade being lower than 6")
else:
print("You have failed due to average grade being lower than 6 and attendance lower than 80%")
The problem is that you're never resetting the value of valid, so once you set valid = True in the first while loop, none of the other loops will execute (because the loop condition will be false).
You don't need the valid variable in any case; instead of having a
conditional loop expression with a flag, just break out of the loop
when you receive a valid value:
while True:
grade1 = float( input("Enter the grade1: ") )
if grade1 <0 or grade1 > 10:
print('Please enter a grade between 0 and 10')
continue
else:
break
while True:
grade2 = float(input("Enter the grade2: "))
if grade2 < 0 or grade2 > 10:
print('Please enter a grade between 0 and 10')
continue
else:
break
while True:
absences = int(input("Enter the number of absences: "))
if type(absences)!=int:
print("Number of absences can only be an integer value")
continue
else:
break
while True:
total_classes = int(input("Enter the total number of classes: "))
if type(total_classes)!=int:
print("Total number of classes can only be an integer value")
continue
else:
break
Unrelated to your question, but when you have a boolean variable you
don't need to compare it explicitly to False (or True); you can
use the value of the variable itself as the condition:
while somevar == False:
You can just write:
while not somevar:
This code shows run time error as NZEC. Tried both the ways by input() and raw_input still shows error.
# A = int(input('Enter the amount of A: '))
# B = int(input('Enter the amount of B: '))
A, B = raw_input().split(" ")
A = int(A)
B = int(B)
if (A > 0 and B > 0):
print('The mixture is a solution: ')
elif (A == 0):
print('The mixture is Liquid: ')
elif (B == 0):
print('The mixture is Solid: ')
else:
print('Invalid Entry')
In Python 3 there are no raw_input because all inputs are raw_input.
Given that the code works correctly and doesn't throw an error:
A = int(input('Enter the amount of A: '))
B = int(input('Enter the amount of B: '))
if (A > 0 and B > 0):
print('The mixture is a solution: ')
elif (A == 0):
print('The mixture is Liquid: ')
elif (B == 0):
print('The mixture is Solid: ')
else:
print('Invalid Entry')
I just created a simple calculator program using python language, but there is a little problem here, when I end the program by inputting number 1, there is always the text none.
The question is how do I get rid of the text none in the program that I created? Because to be honest it is very annoying and will damage the image of the program that I created.
def pilihan():
i = 0
while i == 0:
print('\n\tWelcome to the Simple Calculator Program')
print("\nPlease Select Existing Operations", "\n1. subtraction", "\n2. increase", "\n3. division", "\n4. multiplication")
pilihan2 = int(input('Enter your choice (1/2/3/4): '))
if pilihan2 == 1:
angka1 = int(input('Enter the First Number: '))
angka2 = int(input('Enter the Second Number: '))
print(angka1, "-", angka2, "=", angka1 - angka2)
elif pilihan2 == 2:
angka1 = int(input('Enter the First Number: '))
angka2 = int(input('Enter the Second Number: '))
print(angka1, "+", angka2, "=", angka1 + angka2)
elif pilihan2 == 3:
angka1 = int(input('Enter the First Number: '))
angka2 = int(input('Enter the Second Number: '))
print(angka1, ":", angka2, "=", angka1 / angka2)
elif pilihan2 == 4:
angka1 = int(input('Enter the First Number: '))
angka2 = int(input('Enter the Second Number: '))
print(angka1, "x", angka2, "=", angka1 * angka2)
else:
print('Error option, please try again')
continue
print('Program finished, want to restart?')
y = 0
while y == 0:
ulang = int(input('Type 0 for YES and 1 for NO = '))
if ulang == 0:
y += 1
break
elif ulang == 1:
y += 2
break
else:
print('\nThe command you entered is an error, please try again')
continue
if y == 1:
continue
else:
break
print(pilihan())
Change the print(pilihan()) to pilihan(), the return value of pilihan() is None :)
I am getting some syntax error in sublime text and red highlight on elif command in my text.
i am a beginner in coding so their may be some problem that i can't notice.
1
pylint: error E0001 - invalid syntax (<unknown>, line 49) (syntax-error)
print("-------------Welcome User-------------")
choice = 0
while TRUE:
print("1.Square.")
print("2.Rectangle.")
print("3.Circle.")
choice = int(input("Enter your choice : "))
if choice == 1:
print("1.Area.")
print("2.Perimeter.")
ch = int(input("Enter your choice : "))
if ch == 1:
l = float(input("Enter the length : "))
area = l * l
print("The area is : ",area)
elif ch == 2:
l = float(input("Enter the length : "))
per = 4 * l
print("The perimeter is : ",per)
else:
print("Wrong choice\n")
i = input()
elif choice == 2:
print("1.Area")
print("2.Perimeter.")
ch = int(input("Enter your choice : "))
if ch == 1:
l = float(input("Enter the length : "))
b = float(input("Enter the breadth : "))
area = l * b
print("The area is : ",area)
elif ch == 2:
l = float(input("Enter the length : "))
b = float(input("Enter the breadth : "))
per = 2*(l + b)
print("The perimeter is : ",per)
else:
print("Wrong Choice\n")
i = input()
elif choice == 3:
print("1.Area")
print("2.Circumference")
ch = int(input("Enter your choice : ")
if ch == 1:
r = float(input("Enter the radius : "))
print("The area is : ",r*r*3.14)
elif ch == 2:
r = float(input("Enter the radius : "))
print("The perimeter is : ",2*3.14*r)
else:
print("Wrong choice.")
i = input()
In the elif choice == 3 block, you have this line:
ch = int(input("Enter your choice : ")
That line causes the error, because you're missing the closing parentheses on the end.
I wanted to add a confirmation for my code so that the user will be able to change their input. I haven't yet added the loop for the question but for some reason when I run this it fails to run def calc() and therefore the calculations I created cannot continue. Help!!
n=0
x=0
while True:
numbers = input("Please enter a, b,c, or, d to select a equation")
if numbers == "a":
n =3
x = 2
break
elif numbers =="b":
n = 4
x = 5
break
elif numbers == "c":
n=5
x=6
break
elif numbers == "d":
n=6
x=7
break
else:
print("Please enter a,b,c, or d")
w = float(input("Please enter weight"))
choice = input("Is this your answer?")
if choice == "yes":
def calc():
if n > w :
weight = (x - w)
weight2 = weight/n
print(weight)
elif w < x:
weight = (w - x)
weight2 = weight/n
print(weight)
calc()
elif choice =="no":
print ("Lets change it")
else:
print("Please respond with 'yes' or 'no'")