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.
Related
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)))
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!
a = float(input("Insert a floating point number:"))
n = int(input("Insert an integer number >= 0 :"))
accum = 1
count = 1
while n >= count and n >= 0:
accum = accum * a
count += 1
elif n < 0:
print("Integer value is less than 0")
if n >=0 and n < count:
print(accum)
I need to create a code which asks the user for a floating point number 'a' then an integer to use as the power of 'a' which is 'n'. I need to use a while loop and it must be valid for n>=0 only. If I don't have the elif statement then the code runs normally.
While technically you can put n >= 0 in the condition of the while loop to skip the loop if n < 0, it doesn't make sense to do so, because you never modify the value of n in the loop. It would be clearer to put the entire loop in the body of another if statement (which, incidentally, is the one which your elif—or else, as we'll see—would naturally belong to.)
a = float(input("Insert a floating point number:"))
n = int(input("Insert an integer number >= 0 :"))
accum = 1
count = 1
if n >= 0:
while n >= count:
accum = accum * a
count += 1
# We already know n >= 0, and the only way
# for the loop to exit is for n < count to be
# true, so no additional testing needed before
# we call print here.
print(accum)
else: # If n >= 0 is not true, n < 0 *must* be true
print("Integer value is less than 0")
It's invalid because you firstly need to add an if statement and then follow it with an elif.
if n < 0:
print("Integer value is less than 0")
elif n >=0 and n < count:
print(accum)
You need to change the elif and if statement position
while n >= count and n >= 0:
accum = accum * a
count += 1
if n >=0 and n < count:
print(accum)
elif n < 0:
print("Integer value is less than 0")
Else if always comes after if
My program asks the user to enter the number of integers they want to enter. Then the user enters integer. Then the program outputs the max and min of those integers.
I cannot use lists, only loops and conditional statements are allowed.
I tried setting max and min to specific values:
max_num = 0
min_num = 0
print("Please enter the number of integers you want to enter:")
inte_num = int(input())
for val in range (1, inte_sum + 1):
user_num = int(input())
if user_num >= max_num:
max_num = user_num
elif use_num <= min_num:
min_num = user_num
print("max:", max_num)
print("min:", min_num)
The expected output of 1, 2, 3, 4 is max: 4 and min: 1, but the actual output is max: 4 and min: 0
It is because you have initialized max_num and min_num to 0 at first. In the specific example you gave, 1,2,3,4, none of the input is smaller than the initial min_num value which is 0, thus min_num does not change.
One way to solve this problem is to initialize max_num and min_num to None and put values into max_num and min_num if they are None, as the following:
max_num = None
min_num = None
print("Please enter the number of integers you want to enter:")
inte_num = int(input())
for val in range (1, inte_num + 1):
user_num = int(input())
if max_num is None or user_num > max_num:
max_num = user_num
if min_num is None or user_num < min_num:
min_num = user_num
print("max:", max_num)
print("min:", min_num)
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)