Invalid Syntax Python? - python

Why is this code wrong (from Python 3.3.2). All it outputs is 'invalid syntax' when I've looked over the code lots of times:
#Get the numbers from the useer
a = int(input("Enter number a: "))
b = int(input("Enter number b: "))
c = int(input("Enter number c: "))
d = a*b*c #Make d a times b times c
#Display the results
print (str(a) + " mutiplied by " + str(b) + "multiplied by" + str(c) " equals " + str(d)))
This is what it should output:
Enter number a: 5
Enter number b: 10
Enter number c: 3
5 multiplied by 10 multiplied by 3 equals 150
Thanks in advance

You forgot a + operator in that last line, and you have an extra close paren at the end of that line.
print (str(a) + " mutiplied by " + str(b) + "multiplied by" + str(c) " equals " + str(d)))
^^^ ^^^

Related

I want to Remove the addition symbol from in-between the numbers using python and please find the below my code

limit = int(input("Limit: "))
allvalue = ""
count = 0
number = 0
while count < limit:
number += 1
count += number
allvalue += str(number) + " + "
print(allvalue)
This is my output 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
I want the + symbol only in between the numbers.Not to be in the last or the first.
A likely solution is using " + ".join(), which uses the string method on the " + " to collect the values together
>>> values = "1 2 3 4 5".split()
>>> " + ".join(values)
'1 + 2 + 3 + 4 + 5'
limit = int(input("Limit: "))
allvalue = ""
count = 0
number = 0
while count < limit:
number += 1
count += number
if count != limit:
allvalue += str(number) + " + "
else:
allvalue += str(number)
print(allvalue)
Hope this help.
I would like to share with you a sure shot mathematical solution to this problem.
This problem is a typical variation of Sum of n numbers problem, where the sum depicting limit here is already given as input, instead of n.
import math
limit = int(input("Limit: ")) # n * (n + 1) / 2 >= limit
n = math.ceil( ((1 + 4*2*limit)**0.5 - 1) / 2 ) # ((b^2 - 4ac)^(1/2) - b) / 2a where a = b = 1, c = 2*limit
allValue = " + ".join([str(i) for i in range(1, n+1)])
print(allValue)
You don't need both the number and count variables, and by starting from initial value you can add the + before the number.
limit = int(input("Limit: "))
count = 1
allvalue = str(count)
while count < limit:
count += 1
allvalue += " + " + str(count)
print(allvalue)
You could also try using a for loop.
limit = int(input("Limit: "))
allvalue = ""
for i in range(0, limit):
if i+1 == limit:
allvalue += str(i+1)
else:
allvalue += str(i+1) + "+"
print(allvalue)
Here is simple and easy approach, you can try slice in the result string
print(allvalue[:-2])
code:
limit = int(input("Limit: "))
allvalue = ""
count = 0
number = 0
while count < limit:
number += 1
count += number
allvalue += str(number) + " + "
print(allvalue)
print(allvalue[:-2])
output:
result shared : https://onlinegdb.com/HFC2Hv4wq
Limit: 9
1 + 2 + 3 + 4 +
1 + 2 + 3 + 4

Line 7 Type error: Not all arguments converted during string formatting if (integerr % numberr) >= 1: [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 months ago.
name = input("Say your name: ")
integerr = input(name + " pick an integer ")
numberr = input(name + " pick another integer ")
This is where I get my error
if (integerr % numberr) >= 1:
print(integerr + " is divisible by " + numberr)
else:
print(integerr + " is not divisible by " + numberr)
You have two mistakes.
input always returns a string, as Simsteve7 mentioned. You can convert to an integer using int(), and convert to a string using str().
You need to check if integerr % numberr == 0, because that means that numberr divides into integerr evenly; thus integerr is divisible by numberr.
Below is a solution for Python 2:
name = input("Say your name: ")
integerr = int(input(name + " pick an integer "))
numberr = int(input(name + " pick another integer "))
if (integerr % numberr) == 0:
print(str(integerr) + " is divisible by " + str(numberr))
else:
print(str(integerr) + " is not divisible by " + str(numberr))
If you're using Python 3, you can use f-strings. That would look like this:
if (integerr % numberr) == 0:
print(f"{integerr} is divisible by {numberr}")
else:
print(f"{integerr} is not divisible by {numberr}")

How do I make a repeating 'if' statement until a condition is met?

I'm trying to make a python program that runs the Euclidian algorithm. This is my current code:
a = float(input())
b = float(input())
aplha = float(a/b)
omega = float(b/a)
import math
if a > b:
print(str(a) + " = " + str(b) + " * " + str(math.floor(aplha)) + " + " + str(a%b))
elif b > a:
print(str(b) + " = " + str(a) + " * " + str(math.floor(omega)) + " + " + str(b%a))
else:
print("Both numbers have the same value. The greatest common denominator is 1.")
How do I make it so that the if and elif keeps repeating themselves until a%b = b%a = 0?
Here is one way to implement the Euclidean algorithm.
import math
a = float(input())
b = float(input())
# The greatest common denominator of a and b
def gcd(a,b):
while (b != 0):
t = b
b = a % b
a = t
return a
if (a > b):
print(f'{a} = {b} * {math.floor(a/b)} + {a%b}')
else:
print(f'{b} = {a} * {math.floor(b/a)} + {b%a}')
print(f'The greatest common denominator of {a} and {b} is {gcd(a,b)}')
if a==b it is not necessarily true that the GCD is one. Consider a = 150 and b = 150 as a counter example. The greatest common denominator of a and b is 150. gcd(a,b) = 150.
Also a note on print(f'string{var}').
Print f-string is new in Python 3 and really helpful for printing the value of variables. The way it works is
>>> var = 5
>>> print(f'The value of var is {var}')
"The value of var is 5"

Print Letter "A" depending on the input size

I am working on a program for school, and my homework is to create Letter "A" and the size depends on persons input.
I only worked with loops, print, and input, so I work with what I have.
I tried the following code:
v = eval(input("Enter the size of the Letter: "))
for i in range (v):
print(" " * ((v/2)-i) + i * '*' + ((v/2)-i))
..but I get an error.
Using if statements, my code below seems to work. I am not sure if it is possible without them.
v = eval(input("Enter the size of the Letter: "))
for i in range(1, v, 2):
if i == int(v/2):
middle = i * '*'
else:
middle = '*' + (i-2) * ' ' + '*' if i != 1 else '*'
print(" " * int((v/2)-(i/2)) + middle + int((v/2)-(i/2)) * " ")

Python 3.4.2 | Loop input until set amount

Learning the basics of python and I am running across a problem. I'm sure it is a simple fix. I'm trying to get my program to get 20 different inputs before calculating the min, max, etc.
def main():
number = valueInput()
display(number)
def valueInput():
print("Please enter 20 random numbers")
values = []
for i in range(20):
value1 =(int(input("Enter a random number " + str(i + 1) + ": ")))
values.append(value1)
return values
def display(number):
print("The lowest number is:", min(number))
print("The highest number is:", max(number))
print("The sum of the numbers is:", sum(number))
print("The average number is:", sum(number)/len(number))
main()
I can get it to work by repeating this line:
value1 =(int(input("Enter a random number " + str(i + 1) + ": ")))
to the 20th number but there must be a way that makes it shorter and cleaner. Again I am a beginner so any explanation would be much appreciated.
All you have to do to fix your program is remove four spaces:
def valueInput():
print("Please enter 20 random numbers")
values = []
for i in range(20):
value1 =(int(input("Enter a random number " + str(i + 1) + ": ")))
values.append(value1)
return values
The last line in the block above has been unindented by one level. Instead of returning after the first time through the loop, it lets the loop complete, and then returns the resulting list.
Also you can change your:
values = []
for i in range(20):
value1 =(int(input("Enter a random number " + str(i + 1) + ": ")))
values.append(value1)
return values
part with:
values = [int(input("Enter a random number " + str(x + 1) + ": ")) for x in list(range(20))]
return values
You can check out build in functions

Categories