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 7 years ago.
Improve this question
So my problem is that I need to create a simple python program that takes an input of 6 characters from the user as a postal code and takes the first character and matches it to the corresponding province. For example: Alberta postal codes start with a T so i started off my program with:
pCode = {"Alberta": "T"}
a = input("Enter a 6 character postal code: ")
just to start off the dictionary and get an input. The instructions are also that I must not use loops, lists or if statements. How would i go about doing this?
Your dict is the wrong way round. The thing you want to look up (key) goes first in each pair
pCode = {"T": "Alberta"}
a = input("Enter a 6 character postal code: ")
You can get the first character of the post code as a[0]
print(pCode.get(a[0]))
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 11 months ago.
Improve this question
I need help, in a long string, I want to know the the 2 characters after a number
example = "gz15gr123da1az4gr1sd23f168zgre4r6z"
if in example after 1 number == "gr":
so delete "gr"
If I understand your question correctly, what you need is regex:
import re
example = "gz15gr123da1az4gr1sd23f168zgre4r6z"
re.sub("(\d)gr", r"\1", example)
Output
gz15123da1az41sd23f168zgre4r6z
Explnatation
The re.sub function takes three arguments: the first argument is a patter, the second one the replacement, and the last of is the string itself. (\d)gr selects the parts that contain a number followed by gr, then replaces it with the number (\d) itself(removing the gr part).
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
Id codes at a company come in the form x-y-zzzzzz, where x is a digit and y is a letter and zzzzzz represents a string of 6 letters. Write a function which takes in a code as an input (e.g. 3-a-abaabb) and returns the zzzzzz part (e.g. abaabb).
I have no idea how to start and solve this question. any help would be much appreciated. My IDE is pycharm (solving python coding problems) I basically need to create a function which takes the code as an input and will return the last 6 letters
You can use str.spit('-') then search count in code with repeat is equal or not, like below:
def fnd_code(code):
repeat, char, search = code.split('-')
return search.count(char) == int(repeat)
print(fnd_code('3-a-abaabb'))
print(fnd_code('4-a-abaabb'))
Output:
True
False
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 3 years ago.
Improve this question
Code snippet :
task = input("Add task to do ")
list = task.split()
print("Your All task is here ")
for tasks in list :
print(tasks)
for All_tasks in tasks :
print("\n")
I want my list to print in the following pattern
call to do
meeting
sms
The python function split() by default splits your text on spaces. You can add separator inside, for example: task.split(',') which means this function is going to split your text in list by commas.
See more examples on w3schools.
Firstly, Don't use "list" as a variable name.
And suppose your list, l=[aa,bb,cc,dd]. To print each element in a new line, you can do this-
for i in l:
print(i)
It will give the following Output:-
aa
bb
cc
dd
Python creates a new line after each print command, so the above code will work fine.
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'd like to make a program in python that analyzes a users input.
For example if the user writes "Hello, I'm 19 years old", the program can see that
the string has numbers in it and assigns "19" to a variable.
It could also at the same time see that the string has the word "years" in it and makes the variable:
years = 19
I'm pretty new to python and programming in general, so sorry if the question is weird.
Thanks.
You can count the length of the string using function len. str can contain all chars and letters. Try the following code:
word = input("Waiting for input: ")
print("Found "+str(len(word))+" letters!")
Testing it:
Waiting for input: hello
Found 5 letters!
Testing it with numbers and other chars:
Waiting for input: hello123!##
Found 11 letters!
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 8 years ago.
Improve this question
I have a list of data that reads
[['name','emailtype','phonetype'],['john','yahoomail', 'mobile'],['mark','yahoo','landline']]
I can manually pick out the values i.e print dL[0][0] prints name and dL[1][0] emailtype.
Is it possible to isolate all the names from the list. i.e john and mark. With a program / module and then print them
and produce them into something like this:
1) John
2) Mark
so that I can ask for a raw_input and then if I press 1 as selection it produces john as the answer.
so that it reads similar to the nicely written data that I can manually type as above.
You want to slice the list (to ignore the first row), then use a list comprehension to pick out just the first element of each nested list:
[row[0] for row in nested_list[1:]]