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 1 year ago.
Improve this question
Please help me.
I am a noob and the code does not run. The input should be a number which indicates the index where the 0 turns to an 1.
board2 =[0,0,0,0,0,0,0,0,0]
inp = input('Input Number 0-8:')
if inp == int():
a = inp
for i in board2:
board2.replace(i[a],1)
return board2
You can check if the value input is both an int and if it is in range that you described.
Then you can use the index to replace the value in board2 to 1.
Also, the input() function defaults to string.
board2 =[0,0,0,0,0,0,0,0,0]
inp = input('Input Number 0-8:')
if 0 <= int(inp) < len(board2):
board2[int(inp)] = 1
print(board2)
Almost there! you don't really need to use "replace" here; you can just modify the element which resides at the index provided directly. The only criteria you would need to consider is that the number is greater than zero AND within the range of the "board2" list provided.
board2 =[0,0,0,0,0,0,0,0,0]
inp = input('Input Number 0-8:')
if (0 < int(inp) < len(board2)):
board2[int(inp)] = 1
print(*board2)
This code snippet should solve your question:
board2 = [0,0,0,0,0,0,0,0,0]
index = int(input('Input Number 0-8:'))
if index in range(len(board2)):
board2[index] = 1
print(board2)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
This code verifies if number is in the list.
n = input('enter number: ')
list = [1,2,3,4,5,6,7,8,9,10]
if n in list:
print(True)
print('position')
else:
print('try again')
How to find position of the number in the list using binary search?
You need to convert n to an integer otherwise it won't be found in the list:
n = int(input('enter number: '))
>>> list = [1,2,3,4,5,6,7,8,9,10]
>>> "1" in list
False
>>> 1 in list
True
Also to get the index, use list.index(n)
You can use list.index(item) function which returns the zero-based index of first match of item in the list if it exists, else raises ValueError if it does not exist.
n = 10 #
n = int(input('enter number: '))
list = [1,2,3,4,5,6,7,8,9,10]
if n in list:
print(True)
print(list.index(10))
else:
print('try again')
output -
True
9
As pointed out by #Cubix48, you need to also cast your input into int as well before doing the matching since it will be of str type otherwise.
You should avoid built-in types(list) as names of your variables.
In the updated question, you want to find the index using binary search. This will only work your list is sorted already, you can use the bisect-left which would return the insertion point for item in list to maintain sorted order.
from bisect import bisect_left
n = 10
my_list = [1,2,3,4,5,6,7,8,9,10]
print(bisect_left(my_list, n))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
As the title says, I have created a program that does the following until the user presses the 'X' key. Which then stops and prints out all values in order. I also would like to mention that I can only use a specific compiler through my university, which registers the program as an error/wrong even though it should be correct.
Edit: Added the Status of the compiler.
My question is, what other alternatives can I use to code a similar program, or any recommendations in general.
The requested input:
1
2
X
The requested output:
result:[1, 2]
average:1.50
min:1.00
max:2.00
list1 = []
asknumber = str(0)
while asknumber != 'X':
asknumber = input("Enter number: ")
if asknumber == 'X':
break
list1.append(int(asknumber))
big_value = max(list1)
min_value = min(list1)
average = sum(list1) / len(list1)
print("result:", sorted(list1))
print("average:", f'{average:.2f}')
print("min:", f'{min_value:.2f}')
print("max:", f'{big_value:.2f}')
Since a computer is grading your work, the error is likely because you have spaces after your colons.
Someone suggested to use the following to resolve that issue:
print("result:", sorted(list1), sep='')
However, since you are already using f strings in your print statement, you might as well use them for all of it.
You also do not need to calculate the min, max, and average until the loop ends—and since you break the loop manually, you can just use while True:.
list1 = []
asknumber = str(0)
while True:
asknumber = input("Enter number: ")
if asknumber == 'X':
break
list1.append(int(asknumber))
big_value = max(list1)
min_value = min(list1)
average = sum(list1) / len(list1)
print(f'result:{sorted(list1)}')
print(f'average:{average:.2f}')
print(f'min:{min_value:.2f}')
print(f'max:{big_value:.2f}')
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
So I am trying to compare the first item in a list to the second. I want to find out if the first item is equal to, less than, or greater than the second item.Here is what I have so far. I'm stuck at this part :/
numbers = []
for i in range(0,3):
num = input("Please enter an integer: ")
numbers.append(num)
you have this code that will ask the user for 3 integers, and then you are adding them to the list numbers. You need first to convert then to integers by adding int(input(..))
numbers = []
for i in range(0,3):
num = int(input("Please enter an integer: "))
numbers.append(num)
Now we can start comparing the first and the second number of the list:
if numbers[0] > numbers[1]:
print("the first number is bigger than the second")
elif numbers[1] > numbers[0]:
print("the second number is bigger than the first")
else:
print("the first and the second numbers are equal")
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to take a random number from a list that goes from 0 to 9999. When I get that number, I want to compare it to a 4 digit number. Then, if it doesn't work, I want to REMOVE that random number from the list of 0 to 9999
e.g. it picks 9999 randomly and it compares to 3129 so it deletes it from the list and tries again list (range (0000, 9998))
lis = list(range(0000,9999))
import random
num = random.choice(lis)
while int(num) < pin or int(num) > pin:
print(num)
num = random.choice(lis)
count = count + 1
lis = lis.remove(num)
print('Got your pin in ' + str(count))
from time import sleep
sleep(4)
lis = lis.remove(num)
will set your list to 'None' because the remove method does not return anything. So just using
lis.remove(num)
might solve your problem - whatever your problem is. Your post doesn't really contain a clear question.
import random
comp_number = 3129
def get_give_number_from_random_list(comp_number):
numbers_list = list(range(9999))
default = True
while default:
random_number = random.choice(numbers_list)
if comp_number == random_number:
print('Found the {} Number at index {}'.format(comp_number,numbers_list.index(random_number)))
default = False
else:
numbers_list.remove(random_number)
get_give_number_from_random_list(comp_number)
>>>Found the 3129 Number at index 2197
I would also like to point out that your code does not take into account 0000 to 0999 combinations.
This is because python will simply count from 0 to 9999
You are also including single, double and triple numbers eg 7, 99, 167
You can prepare the list a little better and search using strings rather than int.
pin = '0001' # Search using a string
lis = ['{0:04}'.format(num) for num in range(0, 10000)] #use string foramtting to add the 0's
lis = [x for x in lis if len(str(x)) == 4] # take out any numbers that are less than 4 digits long
print(lis[0:10]) #check the first 10 items in the list
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i am writing a program about coin flips i want the user to enter number and for the program to flip a coin that many times.
once the user gives number the program stops
this is what i have
import random
flips = 0
heads=0
tails=0
numFlips = raw_input("Flips ")
while flips < numFlips:
flips += 1
coin = random.randint(1, 2)
if coin == 1:
print('Heads')
heads+=1
if coin == 2:
print ('Tails')
tails+=1
total = flips
print(total)
print tails
print heads
numFlips is a str. You have to convert it to an int first.
numFlips = int(raw_input("Flips "))
Otherwise, your check flips < numFlips will not work, since all ints are 'less than' any string.
(Also, you want to add some error-handling for the case the user enters something other than an integer)
On line
numFlips = raw_input("Flips ")
raw_input() reads a string : http://docs.python.org/2/library/functions.html#raw_input
Convert it to integer by doing int(raw_input("Flips "))
You can also use input() evaluates the string to a python expression, which in this case would evaluate to an int.
EDIT: As pointed out by #bruno desthuilliers, it is unsafe to use input() and should rather just convert the raw_input() to int.