User input into array - python 3 [closed] - python

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 6 years ago.
Improve this question
Hi there i am trying to put a user input into an array, so far i have:
#Decrypt string
def decrypt():
print("Please enter code to be decrypted.")
text = input(">>>")
print("Please enter your key used to encrypt the data.")
key = int(input(">>>"))
#Put input into array
#????
I am tring to get the input and put it in an array so that it can be referenced using
chr(text[1])
To convert it into plain text from ascii (Basic encryption and decryption).
I have found a few posts on this but they are outdated (for python2 etc...).
Thanks!

If you just want to have an indexable list to store user inputs as they come in, you can use the built-in list class and its append method:
keys = list();
texts = list();
def decrypt():
print("Please enter code to be decrypted.")
text = input(">>>")
print("Please enter your key used to encrypt the data.")
key = int(input(">>>"))
texts.append(text)
keys.append(key)
Now, texts[n] will return the nth text value entered by your user.

Related

How to take multiple inputs in a single line in Python? [closed]

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 6 days ago.
This post was edited and submitted for review 5 days ago.
Improve this question
I want to be able to take input from the user in a single line and assign it to multiple variables in Python. Specifically, I want the user to input their name and their profession on the same line, and for the text "I am a" to be printed only after the user has entered their name.
Here's an example of the desired input/output format:
Input prompt 1: "My Name is "
""" wait for the input after the first prompt after that is submitted
print prompt for the second input and wait for the second input"""
Input prompt 2: "I am a "
Output:
Name: John Doe # whatever name the user enters
Profession: software developer # whatever profession the user enters
How can I achieve this in Python without the inputs moving to the next line before both are entered?
I have tried using a blank space as a separator but when one input is taken from the user, the next one moves to the next line.
print("My name is ",end='')
name = str(input())
print(" I am a ",end='')
profession = str(input())
This obviously couldn't be achieved using input().split() method because I want the text "I am a" to be printed only after input 1 is submitted

Assigning input to a variable [closed]

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 2 years ago.
Improve this question
I am trying a simple code but I'm stuck. I want to ask a question to the user and store the answer to an input, and then write it on a file.
Here's the code :
input = open("Wishes.txt", "w")
wish = input("What do you wish ?")
input.write(wish)
print("Thank you")
I get this error : TypeError: '_io.TextIOWrapper' object is not callable
I'm sure it is really easy but I'm a beginner so I don't know where to find a solution. Thank you in advance.
You have hidden the function name input by creating a variable called input. Simply rename the variable.
Also you probably want to append to the file instead of overwriting the whole thing. And you should be using with to properly handle an external resource.
wish = input("What do you wish ?")
with open("Wishes.txt", "a") as file:
file.write(wish + "\n")
print("Thank you")

It's a question from me as I am a beginner in programming ? I want to ask a simple question related to Python [closed]

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 2 years ago.
Improve this question
The question is, i executed a program which told me to input a name. Now whenever I input a number as the input instead of letters or a string. The number gets taken as my name. I want the program to tell me to enter a name and not a number whenever I type in a number as my name instead of a string. Can you please help me with this problem in Python. I want a simple code and should not contain any import functions as I don't know about it ! Only simple begineers code please.
Here's my code:
name = input("Enter a name : ")
print("Hello", name)
Whenever I enter a number as input for example 5, then it prints Hello 5 but I want it to print Please Enter A Valid Name whenever I input a number. Please Help !
One very simple solution is to use the built-in function isnumeric(), which returns true if all characters in the string are numeric.
name = input("Enter a name : ")
if name.isnumeric():
print("That's not a name!")
else:
print("Hello", name)
Another approach is to use isalpha(), which returns true if all characters in the string are letters.
name = input("Enter a name : ")
if name.isalpha():
print("Hello", name)
else:
print("That's not a name!)
Be careful though, as this will reject some strings that are still names, such as "John Smith" or "Jean-Pierre"
Once you have a string, you can use regular expressions to find out if it contains undesirable characters, as per the following transcript:
>>> import re
>>> name = input("Name? ")
Name? Pax
>>> if re.search("[0-9]", name):
... print("Has digits")
...
>>> name = input("Name? ")
Name? Bobby27
>>> if re.search("[0-9]", name):
... print("Has digits")
...
Has digits
This particular case checks whether any digits exist in the string but you can make the regular expression arbitrarily complex if you want more targeted checks to be done.

Python Checking usernames with regular expressions [closed]

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 2 years ago.
Improve this question
I have a task i need some help with. The task is to create a python script that asks the user to enter a desired username. The username has to be as following: "a11aaaaa".
So starting with a letter, 2x numbers, 5x letters. This is the rule for how the username should look and if the given input does not match that, the user shall be able to try again until getting it right.
Thankful for any help!
As I recently learnt from another user on SO, \w includes \d. Therefore, '^\w\d{2}\w{5}$', as suggested by some users here, will match, for example, 12345678.
To fix that, just specify the character class explicitly:
import re
regex = re.compile('^[A-Za-z]\d{2}[A-Za-z]{5}$')
while True:
password = input('Please enter a password: ')
if regex.search(password):
print('Yay! Your password is valid!')
break
else:
print("Oh no, that's not right. You need a letter, then two numbers, then five letters. ", end='')
^\w{2}\d{2}\w{5}$
You can use this handy site to experiment with regex: https://regex101.com/
You can do something like that:
import re
while True:
name = input('Enter your name')
if re.match('^\w\d{2}\w{5}$', name):
break
Try this one:
import re
uname = input("Enter your username: ")
regex = re.compile(r"^[A-Za-z]{1}\d{2}[A-Za-z]{5}$")
if regex.findall(uname):
print ("Valid username")
else:
print ("Invalid username")

File position and word splitter [closed]

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
I have a program that
Takes a text file with several sentences
Asks the user if they want to compress/decompress the file.
If Compress is chosen, the sentence will have all the unique words and the positions of these words needed to recreate the sentence again.
If decompress is chosen, the compressed text file will need to be found and using the position list given and the unique words - decompressed - the several sentences in the files need to be on separate lines.
Here is the code I have managed to create. It is a subroutine and it's rather faulty.
uniqueWords = []
positions = []
file =
def valChoice():
choice = (" ")
while choice not in ["compress", "decompress"]:
choice = input("Choose compress or decompress").lower()
if choice not in ["compress", "decompress"]:
print("Please input compress or decompress")
finalChoice = valChoice()
if finalChoice = ("compress"):
print("This where i get confused..")
elif finalChoice = ("decompress"):
print("This where i get confused..")
What is wrong with this code? How can I fix it?
With my caveat above, I'll take a shot at what I think you're asking.
To compress the file, iterate through the input words. Store each word reference in a dictionary: the word itself is the key, and its position is the value. If the word is already in the dictionary, then add the new position reference to the existing list of references.
Decompression works in reverse: make a sequence of positions and words. Sort that sequence into ascending order. Concatenate the words to make the original text.
Is that the level of help you need right now?

Categories