end_number = -99
num = (input("Enter Nnumber: "))
list_of_numbers = []
list_of_numbers.append(num)
while num != end_number:
num = (input("Enter Nnumber: "))
print("The smallest number was", min(list_of_numbers))
print("The smallest number was", max(list_of_numbers))
I am trying to a have a person enter a bunch of int and stop when they ender -99. Then I'm wanting to print the smallest and largest in they entered.
input() returns a string and end_number is int, either convert the result of input() to int, with
num = int(input("Enter Nnumber: "))
or convert end_number to string, with
end_number = "-99" # Double quotes to represent -99 as a String of characters
end_number = -99
list_of_numbers = []
while True:
num = int(input("Enter Nnumber: "))
if num == end_number:
break
else:
list_of_numbers.append(num)
print("The smallest number was", min(list_of_numbers))
print("The smallest number was", max(list_of_numbers))
Some recaps:
Consider using while True
list_of_numbers.append(num) should be within while loop.
Your input should be int and within while loop.
You should checked whether num is equal to end_number within while
loop.
You need break to stop the loop if num equals end_number
Your loop doesn't stop because num is not an integer when compared to end_number which is int in the beginning. Then it keeps repeating for input because input in while is also a string.
The element in list_of_numbers is a string and it only contains 1 element because it gets appended just once. So you should move your list_of_numbers.append(num) within while loop and convert num to int to get min and max value later.
You might need if and break to stop the loop when num equals end_numbers, otherwise it always gets -99 as min value.
So it goes like this:
while num != end_number:
num = int(input("Enter Nnumber: "))
if num == end_number: break
list_of_numbers.append(num)
The main problem here is data types as input takes default type as string convert it to int to get value.
as "-99"!=-99
end_number = -99
num = int(input("Enter Number: "))
list_of_numbers = []
list_of_numbers.append(num)
while num != end_number:
num = int(input("Enter Number: "))
list_of_numbers.append(num)
print("The smallest number was", min(list_of_numbers))
print("The smallest number was", max(list_of_numbers))
I am trying this
list_of_numbers = []
while (num := int(input("Enter Nnumber: "))) != -99: list_of_numbers.append(num)
print("The smallest number was", min(list_of_numbers))
print("The smallest number was", max(list_of_numbers))
end_number = -99
num = (input("Enter Nnumber: "))
list_of_numbers = []
list_of_numbers.append(num)
while num != end_number:
num = int(input("Enter Nnumber: "))
print("The smallest number was", min(list_of_numbers))
print("The smallest number was", max(list_of_numbers))
Data Types In Python
In python, We have Data Types such as Integers, Strings, Floats and None Types.
Integers are whole numbers such as 5, 1, -234, 100134
Strings are words and sentences such as, "Hello, World!" or "Nice to meet you!"
Floats are decimal numbers, like, 0.342112, 4.98, -12.23
These data types can not be interchanged without the use of certain functions,
For example.
"5" == 5 = False
5 == 5 = True
"5" == str(5) = True.
str() turns a data type into a string.
int() turns a data type into a integer
float() turns a data type into a decimal.
I hope this helps!
Related
# Armstrong Number
num= int(input("Enter a number: "))
a=[]
b=[]
while num>0:
digit=num%10 #Taking the last digit
a.append(digit) # creating a list with the individual digits
num=num//10
for i in a:
numb=i**len(a) #Calculating the power
b.append(numb) # Creating a new list with the numbers powered to length of the digits
summ=sum(b) #Sum of all the digits in the new list "b"
print("Sum is:", summ)
if summ == num:
print("Yes Armstrong No")
else:
print("Not Armstrong")
The last summ==num "if" condition is always returning the else condition.
Eg if my number (num) is 371, then 3^3+7^3+1^3 is also 371 which is original number = sum and hence it's an Armstrong number so it should return "Yes Armstrong No" but it returning "No" (else condition)..I am unable to identify the error as summ==num (is true here).
You are overwriting the number.
You can just write:
num = int(input("Enter a number: "))
sum = 0
for digit in str(num):
sum += int(digit)**len(str(num))
if sum == num:
print("Yes Armstrong No")
else:
print("Not Armstrong")
And it will work.
You should make a copy of your original input:
original_num = int(input("Enter a number: "))
num = original_num
... and check that instead, as you overwrite num a few times:
if summ == original_num:
I cannot use min, boolean or any other keyword or function.
they can enter positive or negative number, so the smallest value will be set as their first number. If they enter 0 as the first number, a program aborted message will appear. Otherwise, they can enter number and then, hit 0. Then a message will pop up stating the smallest number.
def main():
smallest = 0
while smallest == 0 :
num = int(input("Please enter a number "))
if num==0:
print("Program aborted")
elif smallest == 0:
smallest = num
elif num < smallest:
num = smallest
num = int(input("Please enter a number "))
print("Your smallest number was", smallest)
main()
so with this code, it will print two numbers and it will give the smallest. but it shouldn't automatically stop after two numbers, it should stop after 0 is entered.
You don't need to take seperate input for the smallest.
Please use the below code. It will find you the smallest number.
def main():
smallest = None
while True :
num= int(input("Please enter a number "))
if num == 0:
print("Program aborted")
break
elif smallest is None:
smallest = num
elif num < smallest:
smallest = num
print("Your smallest number was", smallest)
main()
Output:
Please enter a number 5
Please enter a number 3
Please enter a number 2
Please enter a number -10
Please enter a number 6
Please enter a number 0
Program aborted
Your smallest number was -10
you can do something like this:
nums = [int(i) for i in input("Enter the numbers seperated by a space:\n" ).split()]
smallest = nums[0]
for num in nums:
if num < smallest:
smallest = num;
print(f"The smallest number out of {nums}, is {smallest}");
what the code does is first it allows you to input a string of numbers, (separated by a space of course), and then takes each number and puts it in a list. Then it temporarily sets the smallest number to the first number in the list, and iterates through the list one by one to check the numbers in the list against the smallest number. If the current number that it is checking is smaller than the smallest number variable, then it becomes the new smallest number. At the end, it prints out the smallest number in a print statement.
oops sorry forgot it had to use a while loop
I'm trying to do a def function and have it add the digits of any number entered and stop when I type the number "0", for example:
Enter the number: 25
Sum of digits: 7
Enter the number: 38
Sum of digits: 11
Enter the number: 0
loop finished
I have created the code for the sum of digits of the entered number, but when the program finishes adding, the cycle is over, but what I am looking for is to ask again for another number until finally when I enter the number "0" the cycle ends :(
This is my code:
def sum_dig():
s=0
num = int(input("Enter a number: "))
while num != 0 and num>0:
r=num%10
s=s+r
num=num//10
print("The sum of the digits is:",s)
if num>0:
return num
sum_dig()
Use list() to break the input number (as a string) into a list of digits, and sum them using a list comprehension. Use while True to make an infinite loop, and exit it using return. Print the sum of digits using f-strings or formatted string literals:
def sum_dig():
while True:
num = input("Enter a number: ")
if int(num) <= 0:
return
s = sum([int(d) for d in list(num)])
print(f'The sum of the digits is: {s}')
sum_dig()
In order to get continuous input, you can use while True and add your condition of break which is if num == 0 in this case.
def sum_dig():
while True:
s = 0
num = int(input("Enter a number: "))
# Break condition
if num == 0:
print('loop finished')
break
while num > 0:
r=num%10
s=s+r
num=num//10
print("The sum of the digits is:",s)
sum_dig()
A better approach would be to have sum_dig take in the number for which you want to sum the digits as a parameter, and then have a while loop that takes care of getting the user input, converting it to a number, and calling the sum_digit function.
def sum_dig(num): # takes in the number as a parameter (assumed to be non-zero)
s=0
while num > 0: # equivalent to num != 0 and num > 0
r = num % 10
s = s + r
num = num // 10
return s
while True:
num = int(input("Enter a number: "))
if num == 0:
break
print("The sum of the digits is: " + sum_dig(num))
This enables your code to adhere to the Single-Responsibility Principle, wherein each unit of code has a single responsibility. Here, the function is responsible for taking an input number and returning the sum of its digits (as indicated by its name), and the loop is responsible for continuously reading in user input, casting it, checking that it is not the exit value (0), and then calling the processing function on the input and printing its output.
Rustam Garayev's answer surely solves the problem but as an alternative (since I thought that you were also trying to create it in a recursive way), consider this very similar (recursive) version:
def sum_dig():
s=0
num = int(input("Enter a number: "))
if not num: # == 0
return num
while num>0:
r= num %10
s= s+r
num= num//10
print("The sum of the digits is:",s)
sum_dig()
I want to check if the user's 3-digit number input has no repeat digits (eg. 122, 221, 212, 555).
num = 0
while True:
try:
num = int(input("Enter a 3-Digit number: "))
if (num % 10) == 2:
print("ensure all digits are different")
This somewhat works, tells me that the numbers 122 or 212 are have repeats, but not for 221, or any other 3-digit number
num = input()
if len(set(num)) != len(num):
print("The number has repeat digits!")
else:
print("No repeat digits")
A %b gives the remainder if a is divided by b. Rather than doing this just take the number as string and check if any of the two characters present in the string are same.
num = 0
while True:
try:
num = (input("Enter a 3-Digit number: "))
if (num[0] == num[1] or num[1]==num[2] or num[2]==num[0])
print("ensure all digits are different")
You can also make use of a dictionary to check whether digits are repeated or not.
Take the remainder as you did in your code (taking mod 10)and add that remainder into the dictionary.
Everytime we take the remainder, we check inside the dictionary whether the number is present or not because if the number is present inside the dictionary, then it is not unique.
Code :
num = int(input())
dictionary = {}
while num > 0:
remainder = num % 10
if remainder in dictionary.keys():
print("Not unique")
break
else:
dictionary[remainder] = True
num = num // 10
numbers = []
first_input = input('Write any number.When you are done just write "done":')
numbers.append(first_input)
while first_input:
input_numb = input("Write next number")
if input_numb == int():
numbers.append(input_numb)
elif input_numb == "done":
print("The largest number is "+max(numbers))
print("The smallest number is "+min(numbers))
break
Can someone look at this code and tell me what I did wrong please? After I put the input numbers I want to print the biggest and smallest number from the list numbers but I don't know why the max function does not return the biggest number, instead it returns the smallest one (just like the min function. Why?
numbers = []
first_input = input('Write any number.When you are done just write "done":')
numbers.append(int(first_input))
while first_input:
input_numb = input("Write next number")
try:
numbers.append(int(input_numb))
except:
if input_numb == "done":
print("The largest number is ", max(numbers))
print("The smallest number is ", min(numbers))
break
else:
print('invalid input!')
out:
Write any number.When you are done just write "done":1
Write next numbera
invalid input!
Write next number2
Write next number3
Write next number6
Write next numberdone
The largest number is 6
The smallest number is 1
int() will return 0:
class int(x, base=10)
Return an integer object constructed from a number or string x, or
return 0 if no arguments are given.If x is a number, return x.__int__(). For floating point numbers, this truncates towards
zero.
In [7]: int() == 0 == False
Out[7]: True
you should use max in a list of number not a list of string, convert string to int before you append it to list
"The largest number is " + max(numbers)
return :
TypeError: Can't convert 'int' object to str implicitly, just use , to concate the string and int.
you should convert your inputs to integers using int(my_input) then add them to the list my_list.append(int(my_input)) and use the max or min functions max(my_list) after getting all the inputs from the user
numbers = []
user_input = input('Write any number.When you are done just write "done": ')
while user_input != "done":
try:
numbers.append(int(user_input))
user_input = input("Write next number : ")
except ValueError:
user_input = input("please enter a valid number : ")
print("The largest number is ", max(numbers))
print("The smallest number is ", min(numbers))