Basically, after I have made an input, it prints the text on a new line. I want it to print the text after the user input on the same line. I don't want to get an input then print it back to the user, I want to just have an input that does nothing but lets the user type something. (I am new to python, so please keep it simple)
This is for a little choice game i'm making for fun. I've tried to do input("Blah", end = " ") but it just tells me input() takes no keywords.
import time
def type(str):
for letter in str:
print(letter, end='')
time.sleep(0.02)
type("My card number is ")
input()
time.sleep(0.5)
type(" and the expiry date is ")
input()
time.sleep(0.5)
type(". Finally,")
time.sleep(0.5)
type(" the three numbers on the back are ")
input()
print(".")
I wanted it to end up like this:
My card number is 1234 and the expiry date is 1234. Finally, the three
numbers on the back are 123.
Instead I got:
My card number is 1234
and the expiry date is 1234
. Finally, the three numbers on the back are 123
.
Related
I just started my programming education with python in school. One of the programs I started with simply asks you your name and then repeats it back to you. I'd like some help with getting an error message to show up if you put in a number rather than letters.
This is what I have:
while True:
name = input("What is your full name? ")
try:
n = str(name)
except ValueError:
print("Please enter your name in letters, not", repr(name))
continue
else:
break
print(name)
You can check the name if only contains letters by using string.isalpha()
in your case you name it n so n.isalpha() will return True or False
for more information:
How can I check if a string only contains letters in Python?
I need to write an algorithm that validates a player's username and checks to see if the name is registered in an external text file.
playerName = input('Please enter a player name')
How do I restrict the user to only being able to enter letters and numbers?
You cannot restrict what the user can type (at least with input) but you can use a while loop to repeat the input request until user gets it right
As #peer said, you can use a regex in a while loop.
There is no way to do it with input command.
Exemple of code:
import re
playerName = '_'
while(not re.match("^[A-Za-z0-9]*$", playerName)):
playerName = input('Please enter a player name (Only letters and numbers)')
print("PlayerName: ", playerName)
EDIT
As #Joe Ferndz wrote in comment, you can use isalnum() method to check if your playerName is alphanumeric, so you don't even need to use regex
playerName = '_'
while(not playerName.isalnum()):
playerName = input('Please enter a player name (Only letters and numbers)')
print("PlayerName: ", playerName)
Challenge:
Need to write a program that asks the user to enter some data, for instance the names of their friends. When the user wants to stop providing inputs, he just presses Enter. The program then displays an alphabetically sorted list of the data items entered. Do not just print the list, but print each item separately, on a different line.
y = []
def alfabetisch( element ):
return element
while True:
user_input = input("Prompt: ")
if user_input != "":
y += [user_input]
if user_input == "":
break
for element in y:
y.sort( key= alfabetisch )
print( element )
I've made some changes and it works 'sometimes' now. If I put in the following input at the 'prompt:'
[Great, Amazing, Super, Sweet, Nice] it gives back: [Great, Great, Nice, Super, Sweet] so that is two times 'Great' and leaves out 'Amazing'
But when I give in the following input at the 'prompt:' [Amorf, Bread, Crest, Draft, Edith, Flood] it gives back: [Amorf, Bread, Crest, Draft, Edith, Flood], so with this particular input it does what I wanted it to do.
What am I doing wrong here, can anyone provide some pointers?
The input of a user is a string.
If the user doesn't input anything the output of input() function is '' ( as mentioned in the comments).
If the user inputs more than one item, as you mentioned for instance a list of friend names then iterating over the string will give you all the chars that compose that string.
A more Pythonic way of doing that will be:
user_input = input("Prompt: ")
print('\n'.join(sorted(user_input.split())))
'meals = ("Juice", "Milk", "Bread")
name = input("What do you want in a breakfast? ")
if meals =='bread''milk''juice':
print("I love it ")
else:
print(" I don't want it ")'
You need to compare the user input, which is a string stored in the variable name with meals which is a tuple of strings. To compare the two, you need to include a in logical operator:
name = input("What do you want in a breakfast? ")
if name in meals:
print("i love it")
else:
print("i dont want it")
However, for this code to work, the user input must always be capitalized. Next time, ask a question and state your desired outcome instead of just posting code.
So after I have a file opened, the following code is suppose to read through the lines and only print lines that contain the year that I input and the income code that I input, however for income I have to enter a number 1-4 that represent a line in the word file, how can i write it so that the number recognizes the word
input_year = input("Enter a year:")
print()
print("Choose one of the following:")
print("1 for low income.")
print("2 for lower middle income.")
print("3 for upper middle income.")
print("4 for high income.")
print()
income_code_input = input("Enter income level:")
for line in input_file:
line = line.strip()
country = line[0:50]
income = line[51:56]
vaccination = line[59:60]
region = line[62:83]
year = line[-4:]
if year.startswith(input_year) and income.startswith(income_code_input):
print(line.rstrip())
The data format is not specified so I can only guess at what the income field starts with. This is my guess:
code = {'1':'low ', '2':'lower', '3':'upper', '4':'high'}
if year.startswith(input_year) and income.startswith(code[income_code_input]):
print(line.rstrip())
The above uses a dictionary, code, to translate from the number that the user entered to a string that the income field starts with. Note that
input returns a string so that the keys in the code dictionary are all strings, e.g. '1', not numbers, e.g. 1. Note also that we need to distinguish between 'low' and 'lower'. Consequently, I kept a trailing space after 'low' in the value for code[1].