So far I have...
def main():
list = [8, 25, 10, 99, 54, 3, 61, 24]
print("Your list is: ",list)
new = input("Please choose a number from the list:")
print("Your numbers index is:", list.index(new))
input("Press enter to close program")
So my issue is that I can't figure out how to take the user input and get the index of the users choice. For example, if the user entered 99, my program will return "Your numbers index is:3"
Please help.
The input function returns a string, while your list contains ints. You need to convert the string to int:
new = int(input("Please choose a number from the list: "))
I double checked the following code. Can you try running it?
def main():
list = [8, 25, 10, 99, 54, 3, 61, 24]
print("Your list is: ", list)
new = int(input("Please choose a number from the list:"))
print("Your numbers index is:", list.index(new))
input("Press enter to close program")
main()
Related
I have to ask the user to input a number and if it is in the list, then I have to pop it out of the list. I am still new to Python and this is what I have so far:
Any feedback is appreciated. The problem is that the number I input is not removed and when I input a number that is not on the list, multiple "Number not found" messages print out instead of just one.
numbersList = [-11, -4, 5, 12, 13, 14, 19]
numInput = input("Enter a number: ")
for item in range(len(numbersList)):
if item == numInput:
numbersList.pop(item)
print(numbersList)
else:
print("Number not found.")
The issue is you check every element for your number, hence the reason for the multiple messages.
Simply check if the number is in the list once:
numbersList = [-11, -4, 5, 12, 13, 14, 19]
numInput = int(input("Enter a number: "))
if numInput in numbersList
numbersList.pop(numbersList.index(numInput))
print(numbersList)
else:
print("Number not found.")
You're iterating over the indexes of the array, not the values of the array.
At every iteration, if the conditional isn't met, you run the print Number not found.
numbersList = [-11, -4, 5, 12, 13, 14, 19]
numInput = input("Enter a number: ")
try:
index = numbersList.index(int(numInput))
print("Number found.")
numbersList.pop(index)
except ValueError:
print("Number not found.")
Is this good for you?
numbersList = [-11, -4, 5, 12, 13, 14, 19]
numInput = input("Enter a number: ")
if str(numInput) in str(numbersList):
numbersList.remove(numInput)
print(numInput)
else:
print("Number not found.")
You could check really quickly if an item is in a list with the in keywords.
To remove the item, just use the .remove(item_to_remove) method.
I have a code block which makes the user input Fibonacci numbers. The code block:
numb_list = [0, 1, 2, 3, 5, 8, 13, 21, 34, 55]
numb = int(input('Enter the next Fibonacci number >'))
while numb in numb_list and numb <= 50:
numb = int(input('Enter the next Fibonacci number >'))
if numb in numb_list:
print('Well done')
else:
print('Try again')
I am asking the user to input these numbers. When the user input goes over 50 or enters all the correct numbers the program gives output 'Well done'. If user input makes a mistake the program outputs 'Try again'. This is working perfectly but how will i make the user input follow this list in this specific order and if its not in this order the program outputs 'Try again'.
This is the current output:
Enter the next Fibonacci number >1
Enter the next Fibonacci number >1
Enter the next Fibonacci number >2
Enter the next Fibonacci number >3
Enter the next Fibonacci number >8
Enter the next Fibonacci number >3
Enter the next Fibonacci number >
This is the output i would like to achieve:
Enter the next Fibonacci number >1
Enter the next Fibonacci number >1
Enter the next Fibonacci number >2
Enter the next Fibonacci number >3
Enter the next Fibonacci number >8
Enter the next Fibonacci number >3
Try again
Unfortunately I am having trouble achieving this output. Will someone be able to help me?
Thank you!
You can iterate the target number through numb_list instead and use a while loop to keep asking the user for input until the input number matches the target number:
numb_list = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
for target in numb_list:
while int(input('Enter the next Fibonacci number >')) != target:
print('Try again')
print('Well done')
Assuming you want to print Well done every time a correct value is entered, and that you modify your numb_list to have an extra 1 in it (As per the Fibonacci sequence), you can move through the list with an index every time you get the next value in the sequence:
numb_list = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
numb = 0
current_index = 1
while numb <= 50:
numb = int(input('Enter the next Fibonacci number >'))
if numb_list[current_index] == numb:
print('Well done')
current_index += 1
else:
print('Try again')
If you dont want to print Well done every iteration, you can simply remove the print() statement
I am working on a python exercise and here is the question:
Ask the user for a number and return a list that contains only elements from the original list a that are smaller than that number given by the user.
This is the original list:
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
I have the following code:
b = list()
num = raw_input('Give me a number: ')
for i in a:
if i < num:
b.append(i)
print b
But no matter what number I entered it always returns all numbers from the original list into the new list.
Any suggestion or comment?
You need to cast your input to an int, otherwise you are going to be comparing int to str. Change your input prompt to:
num = int(raw_input('Give me a number: '))
I want to know how to get the biggest 'int(input) from the user in a loop. For example:
def main():
times = int(input('How many times will you do this: ')
for i in range(times):
num = int(input('Enter a number: ')
main()
I want to know how I could get the biggest number from the user's input from something like this and print it.
If I said I wanted to enter a number 5 times and entered 6, 1, 21, 34, 3. I would want it to print 34 because that is the biggest value entered by the user.
This is just an example!
Keep track of the numbers the users enters, after the loop, pick the highest one using the max function.
def main():
numbers = []
times = int(input('How many times will you do this: '))
for i in range(times):
num = int(input('Enter a number: '))
numbers.append(num)
print(str(max(numbers)) + ' is the biggest number.')
main()
Created a program for an assignment that requests we make a program that has the user input 20 numbers, and gives the highest, lowest etc. I have the main portion of the program working. I feel like an idiot asking this but I've tried everything setting the max number of entries and everything I've tried still lets the user submit more than 20. any help would be great! I tried max_numbers = 20 and then doing for _ in range(max_numbers) etc, but still no dice.
Code:
numbers = []
while True:
user_input = input("Enter a number: ")
if user_input == "":
break
try:
number = float(user_input)
except:
print('You have inputted a bad number')
else:
numbers.append(number)
for i in numbers:
print(i, end=" ")
total = sum(numbers)
print ("\n")
print("The total amount is {0}".format(str(total)))
print("The lowest number is {0}".format(min(numbers)))
print("The highest number is {0}".format(max(numbers)))
mean = total / len(numbers)
print("The mean number is {0}".format(str(mean)))
Your question could be presented better, but from what you've said it looks like you need to modify the while condition.
while len(numbers) < 20:
user_input = input("Enter a number:" )
....
Now once you've appending 20 items to the numbers list, the script will break out of the while loop and you can print the max, min, mean etc.
Each time the user enters input, add 1 to a variable, as such:
numbers = []
entered = 0
while entered < 20:
user_input = input("Enter a number: ")
if user_input == "":
break
else:
numbers.append(number)
try:
number = float(user_input)
except:
print('You have inputted a bad number')
continue
for i in numbers:
print(i, end=" ")
total = sum(numbers)
print ("\n")
print("The total amount is {0}".format(str(total)))
print("The lowest number is {0}".format(min(numbers)))
print("The highest number is {0}".format(max(numbers)))
mean = total / len(numbers)
print("The mean number is {0}".format(str(mean)))
entered+=1
Every time the while loop completes, 1 is added to the variable entered. Once entered = 20, the while loop breaks, and you can carry on with your program. Another way to do this is to check the length of the list numbers, since each time the loop completes you add a value to the list. You can call the length with the built-in function len(), which returns the length of a list or string:
>>> numbers = [1, 3, 5, 1, 23, 1, 532, 64, 84, 8]
>>> len(numbers)
10
NOTE: My observations were conducted from what I ascertained of your indenting, so there might be some misunderstandings. Next time, please try to indent appropriately.