Hello I am new to python and am trying to work out a way where I can get a list of integers from a user , then a single integer from a user , then check how many times this integer appears in the list of integers. so far i have :
list1 = int(input("how hany items are in your list?"))
list2= []
for i in range(list1):
list_item = input("enter your list of numbers")
list2.append(list_item)
checker1 = int(input("enter a number to check if its in your list"))
checker2 = list2.count(checker1)
print("numbers of items your integer appears in the is is" checker2 "times" )
this is as far as i could get but cant get the code to work. I know that in the print statement you can't concatenate a sting and an integer but I dont now how to go about making this work as i am new. any help would be great! thank you
CodePudding user response:
You can use F strings feature in python
list1 = int(input("how hany items are in your list?"))
list2= []
for i in range(list1):
list_item = input("enter your list of numbers")
list2.append(list_item)
checker1 = int(input("enter a number to check if its in your list"))
checker2 = list2.count(checker1)
print(f"numbers of items your integer appears in the is is {checker2} times" )
Use f""
strings and whenever you want to add a variable then enclose it with {}
within the string quotes.
CodePudding user response:
You have to do 2 things to solve this problem. First: In your for loop, you don't cast the input to an int. You did do that in the first input though. Given that input() returns a string, you have to cast it to an int, otherwise you can't compare them the way you do right now. Furthermore, you should replace the ' 's with commas. Concatenating strings is achieved with commas in python. Result should look something like this:
list1 = int(input("how hany items are in your list?"))
list2 = []
for i in range(list1):
list_item = int(input("enter your list of numbers"))
list2.append(list_item)
checker1 = int(input("enter a number to check if its in your list"))
checker2 = list2.count(checker1)
print("numbers of items your integer appears in the is is", checker2, "times")