Search in Directories with tuples as keys - python [duplicate] - python

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 5 years ago.
I created tuples as keys in dictionary and trying to run a search with user input.
directory = {('a','b'):1, ('c','d'):2, ('e','f'):3}
numsearch = input("enter number to search: ")
if numsearch in directory.values():
print(directory.values())
or
print(directory.items())
does not throw any error or output a value.
Appreciate any help if you tell me the error in my code

directory = {('a','b'):1, ('c','d'):2, ('e','f'):3}
numsearch = input("enter number to search: ")
for k,v in directory.items():
if int(numsearch) == v:
print(k)

Related

My python variables are not being defined [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
What's the canonical way to check for type in Python?
(15 answers)
How to use user input to end a program?
(6 answers)
Asking the user for input until they give a valid response
(22 answers)
Closed 22 days ago.
Here's the beginning of my program:
import sys
print("Enter a number when prompted, type \"stop\" to stop.")
x = input("Input: ")
if type(x) == "int":
Higher = x
Lower = x
elif x.lower() == "stop":
print("The program has been stopped.")
sys.exit()
If right after this I try to print the variable Higher, for example, I get this error message: NameError: name 'Higher' is not defined
EDIT: I tried declaring the variables beforehand but that doesn't work either.

How can I order a list of files correctly? [duplicate]

This question already has an answer here:
How to correctly sort a string with a number inside? [duplicate]
(1 answer)
Closed 1 year ago.
I have a list of files from a directory and I apply sort to order them as in
the_list=os.listdir(dir_name)
img_list = [i for i in the_list if i.endswith('.png') ]
img_list.sort()
My problem is that the files are like this:
file_0.png file_1.png file_2.png ....file_10.png file_11.png ...etc
as a result, my algorithm orders them incorrectly as
'file_0.png', 'file_1.png', 'file_10.png', 'file_100.png', 'file_1000.png', 'file_1001.png', 'file_1002.png', 'file_1003.png', 'file_1004.png',...
How can I order them corretly so that file_2.png comes after file_1.png?
It could be achieved as
img_list.sort(key=lambda name: int(re.match('[^\d]*(\d+).*', name)[1]))

input() function not working in Sublime text [duplicate]

This question already has answers here:
Can't send input to running program in Sublime Text
(5 answers)
Closed 2 years ago.
I have the following function:
years=2
double_rate=(years*12)//3
number_of_rabbits=11
answer=number_of_rabbits*double_rate
print(answer)
I want to generalize the code by being able to input years and number_of_rabbits variables
years=input("select number of years: ")
print(years)
double_rate=(years*12)//3
number_of_rabbits=input("select number of rabbits: ")
print(number_of_rabbits)
answer=number_of_rabbits*double_rate
print(answer)
However the editor (sublime text) only prompts me for the first input variable. I am not able set the second one, "select number of rabbits", nor does it print the new answer
Does anyone know why this is the case?
The number of years you enter is saved as a string, not an int (or float). So when you try to calculate double_rate, you're multiplying a string by 12 (which is fine) and then floor dividing the result by 3, which doesn't work.
Try years = int(input("Select number of years: ")) instead.

How do I import a series of String data into a list, without manually putting the " " sign to each item added to the list? [duplicate]

This question already has answers here:
How do I split a string into a list of words?
(9 answers)
Closed 2 years ago.
I'm a newbie in python.
Basically what I wanted to achieve is:
I have a block of sample data as shown here:
384&cp54,cp170,cp285,cp401,cp517,cp633,cp748,cp864,cp980,cp1096,cp1205,cp1315,cp1424,cp1534,cp1643,cp1753,cp1862,cp1972,cp2082,cp2191,cp2301,cp2410,cp2520,cp2630,cp2739,cp2849,cp2958,cp3068,cp3178,cp3287,cp3342,cp3397,cp3451,cp3506,cp3561,cp3616,cp3671,cp3725,cp3780,cp3835&hp21,hp37,hp49,hp58,hp66,hp73,hp79,hp85,hp91,hp96,hp101,hp105,hp109,hp113,hp117,hp121,hp125,hp129,hp132,hp136,hp139,hp142,hp146,hp149,hp152,hp155,hp158,hp161,hp164,hp166,hp168-170,hp172-174,hp176-178,hp180
How do I code using python3 to achieve the following without having to manually to add the " " sign for each item after the comma.
The desired outcome:
datalist = list()
print(datalist)
Results:
['stritem1','stritem2','stritem3','stritem4'....etc]
Something like this should work:
rawData = "384&cp54,cp170,cp285,cp401,cp517,cp633,cp748,cp864,cp980,cp1096,cp1205,cp1315,cp1424,cp1534,cp1643,cp1753,cp1862,cp1972,cp2082,cp2191,cp2301,cp2410,cp2520,cp2630,cp2739,cp2849,cp2958,cp3068,cp3178,cp3287,cp3342,cp3397,cp3451,cp3506,cp3561,cp3616,cp3671,cp3725,cp3780,cp3835&hp21,hp37,hp49,hp58,hp66,hp73,hp79,hp85,hp91,hp96,hp101,hp105,hp109,hp113,hp117,hp121,hp125,hp129,hp132,hp136,hp139,hp142,hp146,hp149,hp152,hp155,hp158,hp161,hp164,hp166,hp168-170,hp172-174,hp176-178,hp180"
datalist = rawData.split(',')
print(datalist)

really basic newbie python [duplicate]

This question already has answers here:
How does my input not equal the answer?
(2 answers)
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
any reason why this doesn't work?
import random
answer = random.randint(2, 4)
print(answer)
txt = input("what number")
if answer == txt:
print("correct")
every time I enter the correct answer, it just comes back empty (or as an else statement if I put it)
I had this working last night although now I cannot work out why it won't, PS. i've just started to learn python this week so please go easy on me
you can not compare string with integer.
import random
answer = random.randint(2, 4)
print(answer)
txt = int(input("what number"))
if answer == txt:
print("correct")

Categories