Invalid Syntax Error in Temperature Conversion Program - python

Alright, so I was working on a simple temperature conversion program but am stuck on an error message I keep getting. Whenever I try to run the program, the F in the line, F = (C * 9/5 + 32), gets highlighted and a window pops up that states "invalid syntax". I have no idea what the issue could be so i'm hoping it's something simple one of you can point out for me. Thanks in advance!
#Menu Interface
def menu():
print("1. C to F")
print("2. F to C")
print("3. Quit")
# C to F
def CtoF():
C = float(input("Temperature in Celsius:")
F = (C * 9/5 + 32)
print (F,"degrees Fahrenheit")
# F to C
def FtoC()
F = float(input("Temperature in Fahrenheit:")
C = (F-32) * 5/9
print (C,"degrees Celsius")
def option():
loop = 1
while loop == 1:
o = input("Choose an option:")
if o = 1:
CtoF()
elif o = 2:
FtoC()
elif o = 3:
loop = 0
option()

You're missing an end parenthesis on your line
C = float(input("Temperature in Celsius:")

Related

Cant get my code to generate enough fields for bingo

I need my program to generate as many playing fields as the entered number of players playing the game. Currently it only generates 1 field. Cant wrap my head around this one.
s = int(input("Sisesta mängijate arv: "))
b = int(input("Sisesta väjaku suurus: "))
b = b + 1
bb = b
c = int(input("Sisesta korrutustabeli suurus: "))
for s in range (1 , s+1):
numberolemas = ['0']
t.write(str(s) + ". väljak \n\n")
print(str(s) + ". väljak \n")
for bb in range (1 , bb):
for b in range (1 , b):
import random
number = random.randint(1, c) * random.randint(1, c)
olemas = True
while olemas:
number = random.randint(1, c) * random.randint(1, c)
if number not in numberolemas:
olemas = False
t.write(str(number))
print(str(number), end = ' ')
if number <= 9:
t.write(" ")
print(" ", end = '')
elif number >= 100:
t.write(" ")
print(" ", end = '')
else: t.write(" ") and print(" ", end = '')
numberolemas.append(number)
b = b + 1
t.write("\n\n")
print(" ")
bb = bb + 1
print(" ")
t.close() ```

TabError: inconsistent use of tabs and spaces in indentation | Coding a simple calculator

I'm trying to code a simple calculator.
Below is the Updated code. But still same error.
running = True
while running:
print("1 Addition \
2 Subtraction \
3 Multiplication \
4 Division \
5 remainder \
6 Power of \
7 Quit")
O = int(input('What Operaton you want to do ? '))
F = float(input('Enter first number: '))
S = float(input('Enter Second number: '))
if O == 1:
R = F + S
print(F,'+',S,'=',R)
elif O == 2:
R = F - S
print(F,'-',S,'=',R)
elif O == 3:
R = F * S
print(F,'*',S,'=',R)
elif O == 4:
R = F / S
print(F,'/',S,'=',R)
elif O == 5:
R = F % S
print(F,'%',S,'=',R)
elif O == 6:
R = F ** S
print(F,'**',S,'=',R)
else:
print('Quit')
running = False
And while running I'm facing the Below error.
$/usr/local/bin/python3.7 file1.py
File "file1.py", line 20
R = F - S ^
TabError: inconsistent use of tabs and spaces in indentation
You're indenting your while statement unnecessarily.
running = True
while running:
should be:
running = True
while running:
There is another problem though:
Instead of:
else O == 7:
print('Quit')
running = False
You should write:
else:
print('Quit')
running = False
else doesn't take any argument. It is simple the set of statements chosen when no if or elif condition is true.

how to select a decimal in python?

I'm trying to make a simple flight plan helper for Microsoft Flight Simulator in Python and i'm trying to turn a number into time
EXAMPLE- if something equals more than 0.6 (hours) in decimals it would add a 1 and minus the 0.6
PROBLEM 2:
How do i restart the code after its finished a question?
APOLOGIES TO EVERYONE CONFUSED :D
print 'Hello and Welcome to Flight planner Helper V1'
print '-'
question_1 = raw_input('time or fuel? ')
if question_1 == 'time':
a = input('nautical miles ')
b = input('average ground speed ')
c = a / float(b)
print c # <--- PROBLEM 1
print 'Hours'
elif question_1 == 'fuel':
c = input('travel time ')
d = input('fuel usage (in litres)')
e = c * d
f = input('number of engines ')
g = e * f
print '%s litres' % (g)
else:
print 'your not a pilot, your a emu'
CLOSE = input('Press ENTER to close')
If c is in hours you can do something like:
hours = int(c)
minutes = int((c - hours) * 60)
seconds = int((c - hours - minutes / 60.0) * 3600))
print "%d:%d:%d" % (hours, minutes, seconds)
To repeat your code just wrap it in a loop like
while (True):
your code here
Or even better put your code into a function and just call the function from the loop.
There are some minor bugs in your code, but it's easy to sort them out:
import math
while True:
print 'Hello and Welcome to Flight planner Helper V1'
print '-'
question_1 = raw_input('time or fuel? ')
if question_1 == 'time':
a = input('nautical miles ')
b = input('average ground speed ')
c = float(a) / float(b)
print math.ceil(c), # <--- PROBLEM 1
print 'Hours'
elif question_1 == 'fuel':
c = raw_input('travel time ')
d = raw_input('fuel usage (in litres)')
e = float(c) * float(d)
f = raw_input('number of engines ')
g = float(e) * float(f)
print '%s litres' % (g)
else:
print 'your not a pilot, your a emu'
again = raw_input('New calculation? y/n ')
if again != "y": break
What do you mean with 0.6? Do you want to round up? math.ceil() does this.
You would need to add some sort of loop to the code so it can keep going / 'restart'.
Try adding a 'while' loop.
while True
Or any while loop you need.

This Python code for a Custom Calculator was working now I broke it and I can't figure out where I went wrong

I had this working and now I cannot figure out what I did to make it not function the way I intend it to. When the program is called by the pyshell it asks the questions as it is supposed to but when the reply is given this is where it loses it mind. It was working and would call the function I have defined but now it will not do so.
I might just redefine the responses to use numbers instead of strings to make it easier and faster to complete the entries, but at the moment it is not working so there is no point in doing that until I can get it figured out.
The error is in the run_time() section, it is not calling the function as it should be, I might be missing something. I want it to call the function when the proper response is given.
I did try:
k = (query())
f = function_list[k]
f()
This did not worth either, so I am stuck. I appreciate any help.
def f_to_c():
temp = int(input("How warm is it out?"))
f_or_c = str(input("Is it C or F?"))
if f_or_c == 'c' or f_or_c == 'C':
updatedtemp = float(temp * 9 / 5 + 32)
print (updatedtemp, 'F')
elif f_or_c == 'f' or f_or_c == 'F':
updatedtemp = float((temp - 32) * 5 / 9)
print (updatedtemp, 'C')
def calc_trapezoid_area():
height = float(Input('What is the height of the trapezoid?'))
length1 = float(Input('What is the length of the bottom base?'))
length2 = float(Input('What is the length of the top base?'))
formula = float((1 / 2 * (length1 + length2)) * height)
print ("The area of the trapezoid is:", (formula))
def cal_circle():
pi = float(245850922 / 78256779)
rad_or_diam = str(input('Diameter or Radius?'))
width = float(input('What is the %s of your Circle?' % rad_or_diam))
if rad_or_diam == 'r' or rad_or_diam == 'R':
print (float(pi * width ** 2))
elif rad_or_diam == 'd' or rad_or_diam == 'D':
print (float(pi * width))
def query():
query = str(input('What would you like to calculate? Degrees, Trapezoids, or Circles?')).lower
function_list = {
"degrees": f_to_c,
"trapezoids": calc_trapezoid_area,
"circles": cal_circle
}
def run_time():
try:
f = function_list[query()]
f()
except KeyError:
print ("You messed up!")
if __name__ == '__main__':
while True:
run_time()
x = str(input('True or False?'))
if x == 'True':
break
There are two problems with your query function:
It doesn't return anything; and
It doesn't call lower.
Instead, try:
def query():
prompt = 'What would you like to calculate? Degrees, Trapezoids, or Circles?'
return input(prompt).lower()
Note the following:
input already returns a string, no need to call str();
You need to explicitly return the value; and
Parentheses () are required to actually call lower, otherwise you return the method itself.
Also, in f_to_c, "flaot" != "float".
Finally, you could improve your run_time function, minimising the amount of code in the try block:
try:
f = function_list[query()]
except KeyError:
print ("You messed up!")
else:
f()
This prevents a KeyError from f() being accidentally silenced.

Syntax error and is there a better way to write this?, with if - else conditional

I have just started learning Python, and I'm trying to self-learn it so I can test out of computer programming, to get to java, which is what I really want to learn. I know this isn't the easiest way to do this, but it's what I came up with. When I run it, there's a syntax error, but I don't really see it. I'm pretty sure I have a lot of things wrong with this.
Might someone help me with my syntax please?
Also, if someone could suggest some way to learn Python, or how they learnt it, because I might get a book on it or something.
File "Triangletest.py", line 8 else tyy == 0
^ SyntaxError: invalid syntax
T1 = input("First side of triangle: ")
T2 = input("Second side of triangle: ")
addi = T1 + T2
suub = T1 - T2
T3 = input("Third side of triangle: ")
tyy = 1
else tyy == 0
if T3 < suub:
pss == 1
else pss = 0
if tyy + pss == 2:
print("The triangle is not possible")
Comments:
T1 = input("First side of triangle: ")
T2 = input("Second side of triangle: ")
addi = T1 + T2
suub = T1 - T2
T3 = input("Third side of triangle: ")
tyy = 1 \ Identation does not work
else tyy == 0 # else requires a : at the end of the line and an if before it it never has a condition. use elif instead of else: if ...:
if T3 < suub:
pss == 1 # you mus ident behind every :
else pss = 0 # this is ok but : missing
if tyy + pss == 2: # there mus be something behind it.
Syntax ok:
T1 = input("First side of triangle: ")
T2 = input("Second side of triangle: ")
addi = T1 + T2
suub = T1 - T2
T3 = input("Third side of triangle: ")
tyy = 1
if False: pass # if before else
elif tyy == 0:
if T3 < suub:
# should it be pss = 1 ?
pss == 1 # this is in the if clause
else: pss = 0
if tyy + pss == 2:
pass # do something
It seems you're trying to solve this: user enters sides, the program answers whether the triangle with those sides is possible. If you sort your sides, it will be much simpler.
sides = [float(input(ord + ' side of a triangle: '))
for ord in 'First Second Third'.split()]
a, b, c = sorted(sides)
if c < a + b: print('Triangle is possible')
else: print('Triangle is impossible')

Categories