Find greatest number entered by four users - python

I am learning python. I need to know, is this also valid way to get an output?
Following code should give me highest user input( highest number among the four numbers)
num1 = int(input("Enter number 1: ")) #user 1
num2 = int(input("Enter number 2: ")) #user 2
num3 = int(input("Enter number 3: ")) #user 3
num4 = int(input("Enter number 4: ")) #user 4
allNum = {num1, num2, num3, num4}
print("All numbers: ",allNum)
if(num1 > num2 and num1 > num4) :
print("User 1's number is greater")
elif(num2 > num3) :
print("User 2's number is greater")
elif(num3 > num4) :
print("User 3's number is greater")
elif(num4 > num2) :
print("User 4's number is greater")
else :
print("done")

With your current method of directly comparing each number, you would do something like this:
if num1 > num2 and num1 > num3 and num1 > num4:
...
elif num2 > num1 and num2 > num3 and num2 > num4:
...
...
Luckily, there is an easier way! You can use Python's built-in max() function, which returns the maximum value of a sequence. That would look something like this:
maximum = max(num1, num2, num3, num4)
if num1 == maximum:
...
elif num2 == maximum:
...
...
The above method works fine, but what if you wanted to use 5 numbers? What about 6? 20? The more numbers you add, the more messy it gets. Each additional number of numbers, you would add another elif statement, which gets tedious. So I would also recommend using some loops and a dictionary for gathering, storing, and comparing the user input.
>>> nums = {}
>>> for i in range(4):
nums[i+1] = int(input(f"Enter number {i+1}: "))
Enter number 1: 4
Enter number 2: 2
Enter number 3: 8
Enter number 4: 10
>>> print("All numbers:", list(nums.values()))
All numbers: [4, 2, 8, 10]
>>> for k, v in nums.items():
if v == max(nums.values()):
print(f"Number {k} is greater")
break
Number 4 is greater
>>>

Trying to keep it as simple as possible for you to understand
num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))
num3 = int(input("Enter number 3: "))
num4 = int(input("Enter number 4: "))
#Store all numbers in list.
allNum = [num1, num2, num3, num4]
#Get the max value of all numbers in that list
maxNum = max(allNum)
#To get the position of this value, use list.index() function
maxIndex = allNum.index(max(allNum))
#Lets say your numbers are [3,5,4,1]. .index() would give you "1" because the index starts from 0 to length of list -1.
#In this case, it would be 0-3. So, if you want `Number 2` is greater instead, then you just add 1 to the maxIndex.
print("Greatest number is number "+str(maxIndex+1))

You can index and max to solve this.
a = []
for i in range(4):
a.append(int(input(f"Enter number {i+1}: ")))
print(f"Number {a.index(max(a)) + 1} is greater")

This is the code i wrote .. I'm just learning python
n1 = int(input("Enter 1st Number: "))
n2 = int(input("Enter 2nd Number: "))
n3 = int(input("Enter 3rd Number: "))
n4 = int(input("Enter 4th Number: "))
if (n1>n2) and (n1>n3) and (n1>n4):
print("n1 is Greater")
elif (n2>n1) and (n2>n3) and (n2>n4):
print("n2 is Greater")
elif (n3>n2) and (n3>n1) and (n3>n4):
print("n3 is Greater")
else:
print(n4,"is Greater")

Related

Printing an extra number

I'm asking the user to enter a number, then printing every even number up to the number entered. problem is if the inputed number is odd then the program prints an extra number.
What's happening?
num1 = int(input("Please enter a number : "))
num2 = 0
while num1 > num2:
num2 += 2
if num2 % 2 == 0:
print(num2)
else:
num2 -= 2
print(num2)
Let's settle this problem first step by step
If else statement is not needed
Your if else checks if num2 is even, but since num2 will always start at 0, and then be incremented by 2, it will always be even.
So this is what your code currently does :
num1 = int(input("Please enter a number : "))
num2 = 0
while num1 > num2:
num2 += 2
print(num2)
This should explain better why the program prints an extra number for an even input number.
Correct version
A very simple way to correct this code, would be to slightly change the while condition and add +1 to num2
num1 = int(input("Please enter a number : "))
num2 = 0
while num1 > num2 + 1:
num2 += 2
print(num2)
So here, it will print even numbers from 2 to num1
If num1 is even, it stops at num2 == num1 + 1
If num1 is not even, it stops at num2 == num1 + 2
num1 = int(input("Please enter a number: "))
num2 = 0
while num1 > num2:
if num2 % 2 == 0:
num2 += 2
if num2 > num1:
break
print(num2)

Recursion output isn't printing

I'm new to python and trying to understand recursion. I'm trying to write a code where someone inputs 2 numbers (Num1, Num2). A calculation will take place until Num1 is greater than Num 2. The result of the calculation's final value should then be outputted.
This is my code:
def Recursive(Num1, Num2):
if Num1 > Num2:
return(10)
else:
if Num1 == Num2:
return(Num1)
else:
return(Num1+Recursive(Num1 * 2, Num2))
Num1=input("Enter number 1: ")
Num2=input("Enter number 2: ")
print("Final value: ", Recursive(Num1, Num2))
This is the output that comes out:
Enter number 1: 1
Enter number 2: 15
That's it. There's no output of my print statement. I'm confused as to what I'm doing wrong here and what I should do.
def Recursive(Num1, Num2):
if Num1 > Num2:
return 10
if Num1 == Num2:
return Num1
print(Num1, Num2)
return Num1 + Recursive(Num1 * 2, Num2)
Num1=int(input("Enter number 1: "))
Num2=int(input("Enter number 2: "))
print("Final value: ", Recursive(Num1, Num2))
This should be working code, the reason why your code was not working was because without the int in Num1 = int( input("") ) the program was reading the number as a string. As a result instead of getting 2 from Num1 * 2 when Num1 is 1 you got 11 as it was multiplying a string instead of an integer.
# Here is an example
a = input("Input a number: ")
b = int(input("input a number: "))
print(f"a is a:{type(a)}\n b is a:{type(b)}")
print(f"{a * 2}\n{b * 2}")
Copy the code above input the two numbers and it should give you a better understanding of what I just said.

Python programming question - numbers and integers and division

Divisible challenge
Write a program that asks the user for two numbers. The program should output whether the two numbers are exactly divisible by each other. If not, it should return the remainder. If the user enters a 0 the program should give an error message.
so far I've done this:
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
answer1 = num1/num2
answer2 = num2/num1
if num1/num2 == answer1(int) or num2/num1 == answer2(int):
print("Exactly divisible")
elif num1 == 0 or num2 == 0:
print("Error: you cannot divide by 0")
elif num1/num2 != answer1(int) or num2/num1 != answer2(int):
print("Not Exactly divisible")
please help...
I would approach this way
first check the zero case, in that case the code terminates faster
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
# answer1 = num1/num2
# answer2 = num2/num1
def divisible_by_each_other(num1, num2):
if (num1 == 0) or (num2==0):
print("Error do not enter 0")
#check divisibility
elif (num1%num2 == 0 ) or (num2%num1 == 0 ):
print("Exactly divisible")
else:
print("Not Exactly divisible")
high,low= max(num1,num2),min(num1,num2)
print("The remainder is ", high%low)
divisible_by_each_other(num1, num2)
answer1(int) is not the correct way to convert to an integer, it's int(answer1).
Since you already divided the numbers, you could use
if answer1 == int(answer1) or answer2 == int(answer2):
But you're making this much more complicated than it needs to be. Modulus is a simpler way to test divisibility.
if num1 % num2 == 0 or num2 % num1 == 0
You also should check if either of the numbers is zero before you try to use division or modulus. Otherwise, you'll get an error due to trying to divide by 0, which is what you're trying to prevent with this check.
If they're not divisible by each other, get the remainder of the larger divided by the smaller:
else:
return max(num1, num2) % min(num1, num2)
Here's the full, simplified code:
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
higher = max(num1, num2)
lower = min(num1, num2)
if num1 == 0 or num2 == 0:
print("Error: you cannot divide by 0")
elif higher % lower == 0
print("Exactly divisible")
else:
print(higher % lower)

Minimum of three numbers, getting wrong answer for some values [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 5 years ago.
I have tried to find smallest of three numbers but for some values the answer is wrong like 10,30,4 for example and it shows smallest number to be 10.
num1 = input("Enter a number: ")
num3 = input("Enter a number: ")
num4 = input("Enter a number: ")
if (num1 < num3) and (num1 < num4):
print(num1)
elif (num3 < num4) and (num3 < num1):
print(num3)
else:
print(num4)
You are comparing text. You need to convert the input to int objects before comparing.
num1 = int(input("Enter a number: "))
num3 = int(input("Enter a number: "))
num4 = int(input("Enter a number: "))
if (num1 < num3) and (num1 < num4):
print(num1)
elif (num3 < num4) and (num3 < num1):
print(num3)
else:
print(num4)
Problem: input() returns a string, and strings are compared lexicographically. So you should convert it into an integer, you can do this with int(..):
num1 = int(input("Enter a number: "))
num3 = int(input("Enter a number: "))
num4 = int(input("Enter a number: "))
if (num1 < num3) and (num1 < num4):
print(num1)
elif (num3 < num4) and (num3 < num1):
print(num3)
else:
print(num4)
But nevertheless this is not a good way to calculate the minimum. What will you do if you have to calculate the minimum of 50 integers? Construct a hug amount of checks? Computer scientist Edsger W. Dijkstra once said:
"Two or more? Use a for!" - Edsger W. Dijkstra
So in case you have to do work multiple times, you better use a for loop to handle that. The skeleton of such code fragment looks like:
for i in range(3):
# ...
pass
The body of the for loop (here ...) will be repeated three times. Now what work can we put into the for loop? Of course we can query the user for input. So we can write:
for i in range(3):
number = int(input("Enter a number: "))
# ...
but now we still have to calculate the minimum. A way to handle this is by using an accumulator: a variable that is maintained through iteration, and updated accordingly. Here an accumulator can be the thus far obtained minimum. Since initially we have no minimum, we can initialize it with None:
thus_far_minimum = None
for i in range(3):
number = int(input("Enter a number: "))
# ...
Now in case thus_far_minimum is None, we can assign the number we have queried from the user. Furthermore in case thus_far_minimum is no longer None (so it is initialized), but number is less than thus_far_minimum, we can alter thus_far_minimum such that it now has the minimum. For example:
thus_far_minimum = None
for i in range(3):
number = int(input("Enter a number: "))
if thus_far_minimum is None or number < thus_far_minimum:
thus_far_minimum = number
print(thus_far_minimum)
at the end we can print the thus_far_minimum since it is the minimum over all the numbers we have seen.
With input(), you get strings, so convert the input values to integers.
num1 = int(input("Enter a number: "))
num3 = int(input("Enter a number: "))
num4 = int(input("Enter a number: "))
Set a variable to num1, then reset it as you compare num1 to num3 and num4.
smallest = num1
if num3 < smallest:
smallest = num3
if num4 < smallest:
smallest = num4
if num3 < num4:
smallest = num3
print(smallest)

If-Else Statement-Positive and Negative Integers

I am just starting my first computer science class and have a question! Here are the exact questions from my class:
"Write a complete python program that allows the user to input 3 integers and outputs yes if all three of the integers are positive and otherwise outputs no. For example inputs of 1,-1,5. Would output no."
"Write a complete python program that allows the user to input 3 integers and outputs yes if any of three of the integers is positive and otherwise outputs no. For example inputs of 1,-1,5. Would output yes."
I started using the if-else statement(hopefully I am on the right track with that), but I am having issues with my output.
num = int(input("Enter a number: "))
num = int(input("Enter a number: "))
num = int(input("Enter a number: "))
if num > 0:
print("YES")
else:
print("NO")
I have this, but I am not sure where to go with this to get the desired answers. I do not know if I need to add an elif or if I need to tweak something else.
You probably want to create three separate variables like this:
num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))
num3 = int(input("Enter number 3: "))
In your code you only keep the value of the last number, as you are always writing to the same variable name :)
From here using an if else statement is the correct idea! You should give it a try :) If you get stuck try looking up and and or keywords in python.
On the first 3 lines, you collect a number, but always into the same variable (num). Since you don't look at the value of num in between, the first two collected values are discarded.
You should look into using a loop, e.g. for n in range(3):
for n in range(3):
num = int(input("Enter a number: "))
if num > 0:
print("YES")
else:
print("NO")
Use the properties of the numbers...
num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))
num3 = int(input("Enter number 3: "))
Try Something like this
if num1< 0 or num2 <0 or num3 < 0:
print ('no')
elif num1 > 0 or num2 > 0 or num3 > 0:
print ('yes')

Categories

Resources