Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
Hi all,
I am trying to figure out where is went wrong.
The question is:
Write a program whose inputs are three integers, and whose output is the smallest of the three values.
Ex: If the input is:
7
15
3
the output is:
3
So I wrote this:
num_1 = int(input()
num_2 = int(input()
num_3 = int(input()
def smallest (num_1, num_2, num_3)
return min (num_1, num_2, num_3)
if num_1 < num_2 and num_1 < num_3
smallest_num = num_1
elif num_2 < num_1 and num_2 < num_3
smallest_num = num_2
else:
smallest_num = num_3
print(smallest_num)
However, I am getting this syntax error:
Syntax errors detected :
Line 2:
num_2 = int(input()
^
SyntaxError: invalid syntax
What I am doing wrong and how can I fix this?
Thanks in advance!
numbers in one line separated by space character
max(map(int, input().split()))
each number in separate line
N = 3
max(map(int, (input() for _ in range(N))))
close the brackets properly and put : at the end of def fun(...):
num_1 = int(input())
num_2 = int(input())
num_3 = int(input())
def smallest (num_1, num_2, num_3):
return min (num_1, num_2, num_3)
if num_1 < num_2 and num_1 < num_3:
smallest_num = num_1
elif num_2 < num_1 and num_2 < num_3:
smallest_num = num_2
else:
smallest_num = num_3
print(smallest_num)
better approach:
num_1 = int(input())
num_2 = int(input())
num_3 = int(input())
def largest_num(*args):
return max(args)
print(largest_num(num_1, num_2, num_3))
Output:
55
26
66
66
One line approach:
count = 3
print(max(int(input()) for _ in range(count)))
Related
I am getting "low >= high" error in the loop code below, how can it be resolved?
while True:
max_num = 1000
num_1 = np.random.randint(1, max_num)
num_2 = np.random.randint(1, max_num)
if (num_1 < num_2):
num_2 = np.random.randint(1, num_1)
break
In your condition:
if (num_1 < num_2):
num_2 = np.random.randint(1, num_1)
num_1 can equal to 1. In this case this np.random.randint(1, num_1) gives the error, as the low and the high integers are equal.
Your while loop is pointless currently since it will always break on the first iteration.
Since you seem to want two random numbers, where num_1 >= num_2, a simple fix would be just:
max_num = 1000
while True:
num_1 = np.random.randint(1, max_num)
num_2 = np.random.randint(1, max_num)
if (num_1 >= num_2):
break
This will keep looping, picking fresh pairs of random numbers, until the desired condition (num_1 >= num_2) is met.
I don't know how to write a program that is able to get 20 numbers in the input() and returns the biggest number, even if two or more numbers have the same amount of divisor, I want the biggest one for the answer.
def divisor():
n = 0
answer = 0
temp_1 =0
temp_2 = 0
while True:
temp_1=0
n = int(input('Enter your number: '))
if n <=0:
break
for i in range(1, n+1):
if n % i == 0:
temp_1+=1
if temp_1>=temp_2:
if(n > answer):
answer = n
temp_2 = temp_1
my_list = []
my_list.append(n)
for i in range(n):
x = len([i for i in range(1,n+1) if not n % i])
return f'{answer} {x}'
print(divisor())
this is my code but it doesn't work the way I want and I think it's not really readable, I myself got confused using it.
Could you please help me in this in a way that a new python learner could understand it? Thx a lot in advance.
The following code should do what you need:
import numpy as np
def inputNumber(message):
while True:
try:
userInput = int(input(message))
except ValueError:
print("Not an integer! Try again.")
continue
else:
return userInput
break
def divisor(number_of_inputs):
all_numbers = np.zeros(number_of_inputs, dtype='int')
for i in range(number_of_inputs):
all_numbers[i] = inputNumber("Enter your Number: ")
return np.gcd.reduce(all_numbers)
print(divisor(20))
If you have any questions, feel free to ask!
I want to check the value range of an input number, then print the correct "size" (small, medium or large). If the value is out of my acceptable range, then I want the else statement to print out that the number is not valid.
Minimal example for my problem:
n = int(input("number= "))
if 0 <= n < 5:
a = "small"
if 5 <= n < 10:
a = "medium"
if 10 <= n <= 20:
a = "large"
print("this number is",a)
else:
print("thats not a number from 0 to 20")
According to Google, this is a problem with indentation. I've tried multiple ways of indenting this; I can fix the syntax, but I can't get the logic correct.
Let's fix your immediate issue: you have an else with no corresponding if statement. Syntactically, this is because you have an intervening "out-dented" statement, the print, which terminates your series of ifs.
Logically, this is because you have two levels of decision: "Is this a number 0-20?", and "Within that range, how big is it?" The problem stems from writing only one level of ifs to make this decision. To keep close to your intended logic flow, write a general if on the outside, and encapsulate your small/medium/large decision and print within that branch; in the other branch, insert your "none of the above" statement:
n = int(input("number= "))
if 0 <= n <= 20:
if n < 5:
a = "small"
elif n < 10:
a = "medium"
else:
a = "large"
print("this number is", a)
else:
print("that's not a number from 0 to 20")
You should try something like
n = int(input("number= "))
if 0 <= n < 5:
a = "small"
elif 5 <= n < 10:
a = "medium"
elif 10 <= n <= 20:
a = "large"
else:
a = "not a number from 0 to 20"
print("this number is",a)
The print statement before the else statement needs to either be removed or indented to match:
a= "large"
You've syntax (indentation) error:
n = int(input("number= "))
if 0 <= n < 5:
a = "small"
if 5 <= n < 10:
a = "medium"
if 10 <= n <= 20:
a = "large"
#print("this number is",a) indentation error in this line
else:
print("thats not a number from 0 to 20")
You can use also use following code
n = int(input("number= "))
if 10 <= n <= 20:
a = "large"
print("this number is",a)
elif 5 <= n < 10:
a = "medium"
print("this number is",a)
elif 0 <= n < 5:
a = "small"
print("this number is",a)
else:
print("thats not a number from 0 to 20")
The problem is with the print statement.
It is indented on the same level as the if block and thus, the if block ends on line containing the print statement.
Thus, the else on the next line is incorrect.
To achieve what you are trying, you should do something like this:
n = int(input("number= "))
if 0 <= n < 5:
a = "small"
elif 5 <= n < 10:
a = "medium"
elif 10 <= n <= 20:
a = "large"
else:
print("not between 0 and 20")
print("The number is", a)
The print statement needs to be placed after the else block. Also, its best to use elif statements than if statements in a situation like this.
n = int(input("Enter a number between 0 and 20: "))
if 0 <= n <= 5:
a = "small."
elif n <= 10:
a = "medium."
elif n <= 20:
a = "large."
else:
a = "invalid / out of range."
print("This number is ", a)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
The following python coding is written to generate the triangle numbers between 1 to 55. But the coding is not working why ?
num = 1
sum = 0
while (num <= 10)
sum = sum + num
num = num + 1
print (sum, end=' ')
Missing colon :
num = 1
sum = 0
while (num <= 10):
sum = sum + num
num = num + 1
print (sum, end=' ')
or
num = 1
sum = 0
while (num <= 10):
sum = sum + num
num = num + 1
print (sum, end=' ')
Output
1 3 6 10 15 21 28 36 45 55
For 2.7
num = 1
sum = 0
while (num <= 10):
sum = sum + num
num = num + 1
print sum,
Your error is an error that is not followed by a while statement, followed by a () wrap and end of print that are not supported by default in Python 2.x.
The Corrected Code is:
num = 1
sum = 0
while (num <= 10):
sum = sum + num
num = num + 1
print sum
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Essentially, if the user inputs 2,12, the output should be 2 + 3 + 4 + 5 + 6 + 7 + 9 + 10 + 11 + 12.
num1 = int(input("Please enter a number between 1 and 10: "))
num2 = int(input("Please enter a number between 11 and 20: "))
addition = num1 + num2
print (addition)
sum = 0
count = 1
while (count <= num1):
sum = sum + 1
count = count + 1
print ("Your total price comes to ", total_price)
Try the following code:
num1 = int(input("Please enter a number between 1 and 10: "))
num2 = int(input("Please enter a number between 11 and 20: "))
the_sum = 0
start = num1
end = num2 + 1
m = start
while m < end:
the_sum += m
m += 1
print ("Your total price comes to ", the_sum)
You have several problems... including that you are not actually doing anything. All you really do is do the addition line and printing what it is. Naturally, you should start with a for loop that will loop through the limits set by the user:
for i in range(num1, (num2)+1):
Now to add the numbers in between and keep track of the current sum, let's create two variables to keep track:
current_sum = 0
number = num1
for i in range(num1, (num2)+1):
Now add number to current_score and add one to number:
current_sum = 0
number = num1
for i in range(num1, (num2)+1):
current_score += number
number += 1
Then finally print the result:
current_sum = 0
number = num1
for i in range(num1, (num2)+1):
current_score += number
number += 1
print current_sum