My function keeps producing the word "none" in the output - python
I am in a beginners computer science class, and for my final I have to create a menu that includes all of my previous coding assignments using only the basic techniques that we have been taught. I have created a code that works, but the problem is my code also produces the word "none" in the output.
def options():
print ("Pick from the list")
print ("1. Grade Conversion")
print ("2. Temperature Conversion")
print ("3. Currency Conversion")
print ("4. Sum and Average")
print ("5. Heads or Tails Guessing Game")
print ("6. Fibonacci Sequence")
print ("7. Factorials")
print ("8. Multiplication Table")
print ("9. Guess the Number Game")
print ("10. Calculator")
print ("11. Exit")
def Student_A_Gets_A():
print ("Student A gets Grade A")
print (Score)
def Student_B_Gets_B():
print ("Student B gets Grade B")
print (Score)
def Student_C_Gets_C():
print ("Student C gets Grade C")
print (Score)
def Student_D_Gets_D():
print ("Student D gets Grade D")
print (Score)
def Student_F_Gets_F():
print ("Student F gets Grade F")
print (Score)
def Celsius():
Temperature = int(input ("Please enter Temp. in Celsius: "))
print ("Fahrenheit = ", (Temperature*(9/5))+32)
def Fahrenheit():
Temperature = int(input ("Please enter Temp. in Fahrenheit: "))
print ("Celsius = ", (Temperature-32)*(5/9))
def Currency_Conversion():
print ("Pick from the list")
print ("1. Dollar to Euro")
print ("2. Dollar to Peso")
print ("3. Euro to Dollar")
print ("4. Peso to Dollar")
print ("5. Euro to Peso")
print ("6. Peso to Euro")
def Dollar_to_Euro():
print ("You have selected 1 to convert Dollar to Euro")
Amount = int(input("Please enter the amount in dollar(s): "))
print ("$", Amount, " = ", "€",(Amount)*(0.813654))
def Dollar_to_Peso():
print ("You have selected 2 to convert Dollar to Peso")
Amount = int(input("Please enter the amount in dollar(s): "))
print ("$", Amount, " = ", "Mex$",(Amount)*(18.695653))
def Euro_to_Dollar():
print ("You have selected 3 to convert Euro to Dollar")
Amount = int(input("Please enter the amount in euro(s): "))
print ("€", Amount, " = ", "$",(Amount)/(0.813654))
def Peso_to_Dollar():
print ("You have selected 4 to convert Peso to Dollar")
Amount = int(input("Please enter the amount in peso(s): "))
print ("Mex$", Amount, " = ", "$",(Amount)/(18.695653))
def Euro_to_Peso():
print ("You have selected 5 to convert Euro to Peso")
Amount = int(input("Please enter the amount in euro(s): "))
print ("€", Amount, " = ", "Mex$",(Amount)*(22.98))
def Peso_to_Euro():
print ("You have selected 6 to convert Peso to Euro")
Amount = int(input("Please enter the amount in peso(s): "))
print ("$", Amount, " = ", "€",(Amount)/(22.98))
def options2 ():
print ("Select Operation")
print ("1. Add 2 numbers")
print ("2. Subtract 2 numbers")
print ("3. Multiply 2 numbers")
print ("4. Divide 2 numbers")
print ("5. Guessing Game")
print (options())
answer = int(input("Enter a choice from 1 through 10: "))
while (1):
if answer < 1 and answer > 11:
print ("Sorry, that is not an option. Enter a number from 1 through 11")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 1:
Score = int(input("Please enter Score: "))
if Score >= 90 and Score <= 100:
print (Student_A_Gets_A())
print (" ")
print (options ())
answer = int(input("Enter a choice from 1 through 11: "))
elif Score >= 80 and Score <= 89:
print (Student_B_Gets_B())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Score >= 70 and Score <= 79:
print (Student_C_Gets_C())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Score >= 60 and Score <= 69:
print (Student_D_Gets_D())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Score >= 50 and Score <= 59:
print (Student_F_Gets_F())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
else:
print ("Error: Score must be between 100 and 50 for grades A through F")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 2:
Celsius_or_Fahrenheit = int(input ("Please enter 1 for Celsius or 2 for Fahrenheit: "))
if Celsius_or_Fahrenheit == 1:
print (Celsius())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Celsius_or_Fahrenheit == 2:
print (Fahrenheit())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
else:
print ("Error, must choose 1 for Celsius or 2 for Fahrenheit")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 3:
print (Currency_Conversion())
Currency_Conversion_answer = int(input("Enter a choice from 1 through 6: "))
if Currency_Conversion_answer == 1:
print (Dollar_to_Euro())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Currency_Conversion_answer == 2:
print (Dollar_to_Peso())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Currency_Conversion_answer == 3:
print (Euro_to_Dollar())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Currency_Conversion_answer == 4:
print (Peso_to_Dollar())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Currency_Conversion_answer == 5:
print (Euro_to_Peso())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Currency_Conversion_answer == 6:
print (Peso_to_Euro())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
else:
print ("Error, number not within bounds")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 4:
number = int(input("How many numbers would you like to add: "))
counter = 0
average = 0
for j in range(1,number+1):
if j %10 == 0:
print (j,",")
elif j == number:
print (j)
else:
print (j,"," ,end="")
for i in range(1,number + 1):
counter += i
average = counter/number
print ("sum = ", counter)
print ("average = ", average)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 5:
import random
rand = random.randint(1,2)
guess = int(input("Guess Heads(1) OR Tails(2): "))
if guess is 1 or guess is 2 and guess is rand:
print ("You win")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif guess is 1 or guess is 2 and guess is not rand:
print ("You lose")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
else:
print ("Error, number must be 1 or 2")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 6:
fib = int(input("How many Fibonacci numbers shall I generate? "))
x = 1
y = 1
print ("Fibonacci sequence up to", fib)
for i in range (1):
for i in range (1,fib+1):
x = y - x
y = x + y
if i %10 == 0 and i is not fib:
print (x, ", ")
elif i == fib:
print (x)
else:
print (x, ", ", end="")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 7:
Number = int(input("Factorial of what number do you want? Number must be less than or equal to 20. "))
if Number > 20:
print ("Error")
else:
Factorial = 1
for i in range(1,Number + 1):
print (i,)
Factorial = Factorial * i
print("Factorial of", Number, " is ",Factorial)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 8:
num = int(input("Enter multiplication table you want to see: "))
maximum = int(input("Enter max you want your table to go to: "))
counter = 0
print ("The table of", num)
for i in range (0,maximum):
counter = i + 1
print (counter, "*", num, "=", counter*num)
print ("Done counting...")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 9:
import random
number = int(input("Guess any number 1 to 10? "))
guess = random.randint(1,10)
acc = 0
while guess > 0:
if guess < number:
print ("Too High")
number = int(input("Guess any number 1 to 10? "))
acc+=1
elif guess > number:
print ("Too Low")
number = int(input("Guess any number 1 to 10? "))
acc+=1
elif guess == number:
acc+=1
break
print ("And it only took you", acc, "tries")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 10:
import random
rand = random.randint(1,4)
print (options2())
answer = int(input("Enter a choice from 1 through 5: "))
if answer > 5 and answer < 1:
print ("Sorry, that is not an option")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 1:
x=float(input("Please enter the first number: "))
y=float(input("Please enter the second number: "))
print (x,"+",y,"=",x+y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 2:
x=float(input("Please enter the first number: "))
y=float(input("Please enter the second number: "))
print (x,"-",y,"=",x-y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 3:
x=float(input("Please enter the first number: "))
y=float(input("Please enter the second number: "))
print (x,"*",y,"=",x*y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 4:
x=float(input("Please enter the first number: "))
y=float(input("Please enter the second number: "))
if y == 0:
print ("Error, denominator cannot be zero")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
else:
print (x,"/",y,"=",x/y)
elif answer == 5:
x=float(input("Please enter the first number: "))
y=float(input("Please enter the second number: "))
if rand == 1:
print (x,"+",y,"=",x+y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif rand == 2:
print (x,"-",y,"=",x-y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif rand == 3:
print (x,"*",y,"=",x*y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif rand == 4:
print (x,"/",y,"=",x/y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
else:
print ("Error, must choose from the aforementioned selections")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 11:
break
print ("You have exited")
Printing return values from functions, as #Patrick Haugh indicates. For example, line 82:
print (options())
runs the function options(), which prints out the alternatives, and then prints the return value from options(), which is None as you don't specify any return value. Instead code line 82 as:
options()
You need to correct this on a lot of places, and then for other functions as well, e.g. Celsius() etc.
Related
if statement calls out out to one function only, why? [duplicate]
This question already has answers here: How to test multiple variables for equality against a single value? (31 answers) Closed 1 year ago. see this is my code print("""1. ADD 2. SUBTRACT 3. MULTIPLY 4. DIVIDE 5. SQUARE 6. SQUARE ROOT 7. CUBE 8. CUBE ROOT 9. POWER OF A NUMBER 10. FACTORIAL 11. TRIANGLE'S PERIMETER 12. TRIANGLE'S AREA 13. QUADRILATERAL'S PERIMETER 14. PARALLELOGRAM'S AREA 15. RHOMBUS'S AREA 17. RECTANGLE'S AREA 18. SQUARE'S AREA 19. TRAPEZIUM'S AREA 20. CIRCLE'S CIRCUMFERENCE 21. CIRCLE'S AREA 22. QUADRATIC EQUATION 23. SIMPLE INTEREST 24. COMPOUND INTEREST""") while True: user = str(input("Enter GO to continue or Q to exit: ")) if user == 'GO': user_input = str(input("Enter here the serial number or the name in capital for what you want to calculate: ")) if user_input == 'ADD' or 1 : add() elif user_input == 'SUBTRACT' or 2 : sub() elif user_input == 'MULTIPLY' or 3 : mlt() elif user_input == 'DIVIDE' or 4 : div() elif user_input == 'SQUARE' or 5 : sqr() elif user_input == 'SQUARE ROOT' or 6 : sqrt() elif user_input == 'CUBE' or 7 : cube() elif user_input == 'CUBE ROOT' or 8 : cube_root() elif user_input == 'POWER OF A NUMBER' or 9 : power() elif user_input == "TRIANGLE'S PERIMETER" or 11 : triangle_perimeter() elif user_input == "QUADRILATERAL'S PERIMETER" or 13 : quadrilateral_perimeter() elif user_input == "PARALLELOGRAM'S AREA" or 14 : parallelogram_area() elif user_input == 'FACTORIAL' or 10 : factorial() elif user_input == "RHOMBUS'S AREA" or 15 : rhombus_area() elif user_input == "RECTANGLE'S AREA" or 17 : rectangle_area() elif user_input == "SQUARE'S AREA" or 18 : square_area() elif user_input == "CIRCLE'S AREA" or 21 : circle_area() elif user_input == "CIRCLE'S CIRCUMFERENCE" or 20 : circle_circumference() elif user_input == "QUADRATIC EQUATION" or 22 : qaudratic_solver() elif user_input == "SIMPLE INTEREST" or 23 : simple_interest() elif user_input == "COMPOUND INTEREST" or 24 : compound_interest() elif user_input == "TRIANGLE'S AREA" or 12 : triangle_area() elif user_input == "TRAPEZIUM'S AREA" or 19 : trapezium_area() else: print("Invalid input!!") elif user == 'Q': break else: print("Invalid input!!") and when I run it when I put any number or name in the input line instead of calling out the function with respect to the number or name it calls out to the add() function which relates number 1 and and name "ADD" Why is this happening? Please help me... PS: I had done this in google colab so the actual functions are written in a separate "cell" if you know google colab you'll know :) but anyways here are functions too import cmath import functools def add(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': val1 = float(input("Enter the first number: ")) val2 = float(input("Enter the second number: ")) print("The result is: ", val1 + val2) elif user_input == 'Q': break else: print("Invalid input!!") def sub(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': val1 = float(input("Enter the first number: ")) val2 = float(input("Enter the second number: ")) print("The result is: ", val1 - val2) elif user_input == 'Q': break else: print("Invalid input!!") def mlt(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': val1 = float(input("Enter the first number: ")) val2 = float(input("Enter the second number: ")) print("The result is: ", val1*val2) elif user_input == 'Q': break else: print("Invalid input!!") def div(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': val1 = float(input("Enter the first number: ")) val2 = float(input("Enter the second number: ")) print("The result is: ", val1/val2) elif user_input == 'Q': break else: print("Invalid input!!") def sqrt(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': val1 = float(input("Enter the number: ")) print("The result is: ", cmath.sqrt(val1)) elif user_input == 'Q': break else: print("Invalid input!!") def sqr(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': val1 = float(input("Enter the number: ")) print("The result is: ", val1**2) elif user_input == 'Q': break else: print("Invalid input!!") def power(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': val1 = float(input("Enter the number: ")) val2 = float(input("Enter the power of the number: ")) print("The result is: ", val1**val2) elif user_input == 'Q': break else: print("Invalid input!!") def square_area(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': val1 = float(input("Enter the value of the side: ")) print("The area of the square is: ", val1**2) elif user_input == 'Q': break else: print("Invalid input!!") def rectangle_area(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': val1 = float(input("Enter the value of the length: ")) val2 = float(input("Enter the value of the breadth: ")) print("The area of the square is: ", val1*val2) elif user_input == 'Q': break else: print("Invalid input!!") def circle_circumference(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': val1 = float(input("Enter the radius of the circle: ")) print("The circumference of the circle is: ", (val1*44)/7) elif user_input == 'Q': break else: print("Invalid input!!") def circle_area(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': val1 = float(input("Enter the radius of the circle: ")) print("The area of the circle is: ", (val1*val1*22)/7) elif user_input == 'Q': break else: print("Invalid input!!") def rhombus_area(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': val1 = float(input("Enter the value of first diagonal: ")) val2 = float(input("Enter the value of the second diagonal: ")) print("The area of the rhombus is: ", 0.5*val1*val2) elif user_input == 'Q': break else: print("Invalid input!!") def parallelogram_area(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': val1 = float(input("Enter the value of height: ")) val2 = float(input("Enter the value of the correspondent base: ")) print("The area of the parallelogram is: ", 0.5*val1*val2) elif user_input == 'Q': break else: print("Invalid input!!") def quadrilateral_perimeter(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': a = float(input("Enter the first side of the quadrilateral: ")) b = float(input("Enter the second side of the quadrilateral: ")) c = float(input("Enter the third side of the quadrilateral: ")) d = float(input("Enter the foruth side of the quadrilateral: ")) if ((a + b + c> d) and (a + b + d> c) and (b + c + d> a) and (a + d + c> b)) : print("The perimeter of the qaudrilateral is : ", a+b+c+d) else : print("Quadrilateral is not valid.") elif user_input == 'Q': break else: print("invalid input") def trapezium_area(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': par1 = float(input("Enter the value of the first parallel side: ")) par2 = float(input("Enter the value of the second parallel side: ")) hei1 = float(input("Enter the the value of the height: ")) print("The area of the trapezium is: ", 0.5*hei1*(par1+par2)) elif user_input == 'Q': break else: print("Invalid input!!") def triangle_perimeter(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': a = float(input("Enter the first side of the triangle: ")) b = float(input("Enter the second side of the triangle: ")) c = float(input("Enter the third side of the triangle: ")) if ((a + b > c) and (a + c > b) and (b + c > a)) : print("The perimeter of the traingle is: ", a+b+c) else : print("Triangle is not valid.") elif user_input == 'Q': break else: print("invalid input") def triangle_area(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': area_method = str(input("Enter Heron or Traditional(0.5*height*base): ")) if area_method == 'Heron': a = float(input("Enter the first side of the triangle: ")) b = float(input("Enter the second side of the triangle: ")) c = float(input("Enter the third side of the triangle: ")) if ((a + b > c) and (a + c > b) and (b + c > a)) : s = (a+b+c)*0.5 print("The area of the triangle is ", cmath.sqrt((s-a)*(s-b)*(s-c)*(s))) else: print("Triangle is not valid!!") elif user_input == 'Traditional': a = float(input("Enter the value of the height: ")) b = float(input("Enter the value of the correspondent base: ")) print("The area of the triangle is ", 0.5*a*b) else: print("Invalid input!!") elif user_input == 'Q': break else: print("Invalid input!!") def qaudratic_solver(): while True: user_input = str(input("Enter the GO to continue or Q to exit: ")) if user_input == 'GO': a = float(input("Enter the coeffiecient of x^2: ")) b = float(input("Enter the coeffiecient of x: ")) c = float(input("Enter the constant: ")) D = cmath.sqrt(b*b - 4*a*c) F = 2*a X1 = (-b + D)/(F) X2 = (-b - D)/(F) print("The values of x are: ", X1, ", ", X2) elif user_input == 'Q': break else: print("Invalid input!!") def cube(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': val1 = float(input("Enter the number: ")) print("The result is: ", val1**3) elif user_input == 'Q': break else: print("Invalid input!!") def cube_root(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': val1 = float(input("Enter the number: ")) print("The result is: ", val**1/3) elif user_input == 'Q': break else: print("Invalid input!!") def compound_interest(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': P = float(input("Enter the principal amount: ")) IR = float(input("Enter the interest rate: ")) T = float(input("Enter the time period: ")) N = float(input("Enter the number of times interest is compunded per unit time 't': ")) A = P*((1+((IR/100)/N))**(N*T)) CI = A-P print("The final amount will be ", A, " and the compund interest will total up to ", CI) elif user_input == 'Q': break else: print("Invalid input!!") def simple_interest(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': P = float(input("Enter the principal amount: ")) R = float(input("Enter the interest rate: ")) T = float(input("Enter the time period: ")) SI = (P*R*T)/100 A = P + SI print("The final amount will be ", A, " and the simple interest will total up to ", SI) elif user_input == 'Q': break else: print("Invalid input!!") def factorial(): while True: user_input = str(input("Enter GO to continue or Q to exit: ")) if user_input == 'GO': a = int(input("Enter the number: ")) print(functools.reduce(lambda x,y : x * y, range(1,a+1))) elif user_input == 'Q': break else: print("Invalid input!!")
The condition in: if user_input == 'ADD' or 1: doesn't quite do what you think it does. Each side of an or is usually a separate sub-expression that is evaluated independently, then the results are or'ed together. The proper expression for your case would be: if user_input == 'ADD' or user_input == '1': Note the quoted '1' in my version, the string '1' is not the same as the integer 1, and you are working with strings here. However, a more Pythonic way would be: if user_input in ['ADD', '1']: which would allow you to keep code much shorter for things like: if number in [2, 3, 5, 7, 11, 13, 17, 19]: print(f"{number} is a prime under twenty") Interestingly enough, the result of an expression False or something is not necessarily True or False, it's actually something. You can see this with the following transcript: >>> True or 'hello' True >>> False or 'hello' 'hello' More detail can be found here: The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned. The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned. Note that neither and nor or restrict the value and type they return to False and True, but rather return the last evaluated argument. This is sometimes useful, e.g., if s is a string that should be replaced by a default value if it is empty, the expression s or 'foo' yields the desired value. When the something is an expression like a == b, it is a boolean value that comes out. But that's not the or doing that, it's the comparison. That means the result of the expression you've used, user_input == 'ADD' or 1, will be True if the input was ADD, or 1 if not. And it's not until you evaluate that 1 in a truthy context that it gets forced to True. Python's truthiness rules are basically everything is truthy except for: empty strings. empty collections (lists, tuples, dictionaries, sets, frozensets, and ranges). zero of any numeric type (integer, floating point, or complex). None. user defined classed that implement the __bool__ method and decide to return a falsey value. and, of course, False itself. And, just as an aside, you should probably be aware that input() already gives you a string, there's no need to call str() on it. In other words, the expression str(input("What? ")) is about as useful as int(42), harmless but unnecessary :-)
The or statement is to check for another condition. Here with your or 1 you are checking if 1 != 0 (because in boolean is 0 is false and everything else is true). So what you need in the end is to check if user_input is 1. You need to change your conditions line to if user_input == 'ADD' or user_input == '1' : EDIT: as paxdiablo said if user_input in ['ADD', '1'] would be a more pythonic way to do it. Hope this helps and you understood your mistake.
if user_input == 'ADD' or 1 : The above condition is evaluates to true due to 1.
how to count the number of user inputs in Python
Im having loads of trouble trying to count the number of guesses in this after you find the number. def play_game(): print("Enter the upper limit for the range of numbers: ") limit = int(input()) number = random.randint(1, limit) print("I'm thinking of a number from 1 to " + str(limit) + "\n") while True: guess = int(input("Your guess: ")) if guess < number: print("Too low.") elif guess > number: print("Too high.") elif guess == number: print("You guessed it in " + str(count) + " tries.\n") return
Try this: def play_game(): print("Enter the upper limit for the range of numbers: ") limit = int(input()) number = random.randint(1, limit) print("I'm thinking of a number from 1 to " + str(limit) + "\n") count = 1 #new line while True: guess = int(input("Your guess: ")) if guess < number: print("Too low.") elif guess > number: print("Too high.") elif guess == number: print("You guessed it in " + str(count) + " tries.\n") count = count+1 #new line
start with a variable to store the number of guesses ... count = 0 ... then increment it on every guess ... guess = int(input("Your guess: ")) count += 1 ...
You should initialize count as 1 and increment on each loop. import random def play_game(): print("Enter the upper limit for the range of numbers: ") limit = int(input()) number = random.randint(1, limit) print("I'm thinking of a number from 1 to " + str(limit) + "\n") count = 1 while True: guess = int(input("Your guess: ")) if guess < number: print("Too low.") elif guess > number: print("Too high.") elif guess == number: print("You guessed it in " + str(count) + " tries.\n") return count+=1 play_game() An example output: Enter the upper limit for the range of numbers: 10 I'm thinking of a number from 1 to 10 Your guess: 3 Too low. Your guess: 7 Too low. Your guess: 9 Too low. Your guess: 10 You guessed it in 4 tries.
import random highest = 10 answer = random.randrange(1,highest) guess = 0 count = 0 print("please guess a number between 1 and {}".format(highest)) while guess != answer: guess = int(input()) if count == 4: exit(print("you exceeded number of chances")) if guess == answer: print("well done you have guessed it correctly and the answer is {}".format(guess)) break else: if guess < answer: print("please guess higher") else: print("please guess lower") count = count +1
if and else in Python,if age is true grade is false,this program print is wrong
age = float(raw_input("Enter your age: ")) grade = int(raw_input("Enter your grade: ")) if age >= 8: if grade >= 3: print "You can play this game." else: print "Sorry, you can't play the game." if age is true and grade is false,this program prints wrong output.but if age is false, it prints correct output. Why is it happening?
You are leaving open the possibility that age >= 8 but grade < 3 in which you have no control flow to handle. You can correct this succinctly with an and statement age = float(raw_input("Enter your age: ")) grade = int(raw_input("Enter your grade: ")) if age >= 8 and grade >= 3: print "You can play this game." else: print "Sorry, you can't play the game."
age = float(raw_input("Enter your age: ")) grade = int(raw_input("Enter your grade: ")) if age >= 8: if grade >= 3: print"You can play this game." else: print"Sorry , you can't play the game." else: print "Sorry , you can't play the game."
You have to include an 'else' condition in your nested if/else statement: age = float(raw_input("Enter your age: ")) grade = int(raw_input("Enter your grade: ")) if age >= 8: if grade >= 3: print "You can play this game." else: print"Sorry, you can't play this game." else: print "Sorry, you can't play this game."
Python Error on while True loop
total = 0 print ("Enter first number") num1 = input() print ("Enter 1)Add 2)Minus 3)Multiply 4)Divide") choice = input() while True: print("Wrong Answer Pick Again") print("Enter 1)Add 2)Minus 3)Multiply 4)Divide") choice = input() if choice => 1 and choice =< 4: break if choice == 1: print ('Enter second number') num2 = input() total = num1 + num2 elif choice == 2: print ('Enter second number') num2 = input() total = num1 - num2 elif choice == 3: print ('Enter second number') num2 = input() total = num1 * num2 elif choice == 4: print ('Enter second number') num2 = input() total = num1 / num2 print("Total") print (total) I`m getting a syntax error on the "if choice => 1 and choice =<4:" can some one please help. I have tried so many different things and nothing have worked.
This script should work for you : total = 0 print ("Enter 1)Add 2)Minus 3)Multiply 4)Divide") choice = int(input()) for _ in range(int(input("total test cases"))): if choice >= 1 and choice <= 4: if choice == 1: print ("Enter first number") num1 = int(input()) print ('Enter second number') num2 = int(input()) total = num1 + num2 print("Total is: ",total) choice=int(input("enter choice again")) elif choice == 2: print ("Enter first number") num1 = int(input()) print ('Enter second number') num2 = int(input()) total = num1 - num2 print("Total is: ",total) choice=int(input("enter choice again")) elif choice == 3: print ("Enter first number") num1 = int(input()) print ('Enter second number') num2 = int(input()) total = num1 * num2 print("Total is: ",total) choice=int(input("enter choice again")) elif choice == 4: print ("Enter first number") num1 = int(input()) print ('Enter second number') num2 = int(input()) total = num1 / num2 print("Total is: ",total) choice=int(input("enter choice again")) else: print("wrong number entered") choice=int(input("enter again")) Note:If you find this answer helpful you can mark this answer correct
Invalid syntax error on "def" in my coding.
Here is my code: I'm not sure what the problem is. It says invalid syntax on "def sub". I've looked everywhere and as far as I can tell, it is properly formatted for python 3 def add(): print ("Enter the two numbers to Add") A=int(raw_input("Enter A: ")) B=int(raw_input("Enter B: ")) return A + B def sub(): print ("Enter the two numbers to Subtract") A=int(raw_input("Enter A: ")) B=int(raw_input("Enter B: ")) return A - B def mul(): print ("Enter the two numbers to Multiply") A=int(raw_input("Enter A: ")) B=int(raw_input("Enter B: ")) return A * B def div(): print ("Enter the two number to Divide") A=float(raw_input("Enter A: ")) B=float(raw_input("Enter B: ")) return A / B print ("1: ADD") print ("2: SUBTRACT") print ("3: MULTIPLY") print ("4: DIVIDE") print ("0: QUIT") while True: CHOICE = int(raw_input("ENTER THE CORRESPONDING NUMBER FOR CALCULATION ")) if CHOICE == 1: print ('ADD TWO NUMBERS:') print add() elif CHOICE == 2: print ('SUBTRACT TWO NUMBERS') print sub() elif CHOICE == 3: print ('MULTIPLY TWO NUMBERS') print mul() elif CHOICE == 4: print ("DIVIDE TWO NUMBERS") print div() elif CHOICE == 0: exit() else: print ("The value Enter value from 1-4")
All the prints like: print add() are missing the parenthesis. They should be: print(add()) Same for all the prints