sorry about the heading I know it makes no sense. What I was trying to say was when I make an input in e.g.
choice = input ("you have 2 choices: 1)live or ") die")
how do I make each individual choice go onto a new line in my shell without it ending the input when I try to just put it onto a new line the input doesn't recognize the text as part of what I want the input to say. Thanks in advance.
Try this -
choice = input ("you have 2 choices: \n1)live or \n2)die\nChoose: ")
You see the \n? That's called a string literal. See this table for more string literals
Related
I am new to coding. And I would like to know if there's a way for input function to not print newline character after the value is entered. Something like print function's argument end. Is there any way?
Well, you can't make input() trigger by anything besides 'Enter' hit (other way may be using sys.stdin and retrieving character one-by-one until you receive some stop marker, but it's difficult both for programmer and for user, I suppose). As a workaround I can the suggest the following: if you can know the length of line written before + length of user input, then you can use some system codes to move cursor back to the end of previous line, discarding the printed newline:
print("This is first line.")
prompt = "Enter second: "
ans = input(prompt)
print(f"\033[A\033[{len(prompt)+len(ans)}C And the third.")
\033[A moves cursor one line up and \033[<N>C moves cursor N symbols right. The example code produces the following output:
This is first line.
Enter second: USER INPUT HERE And the third.
Also note that the newline character is not printed by your program, it's entered by user.
name=input('Enter your name : ')
print('hello',name,end='')
I know about the end function which is abov
In my code I am trying to make an etch-a-sketch. I am in a coding class at my highschool and our project is to use lists and user inputs to make whatever we wanted to do. I chose etch-a-sketch because our class is also doing drawing with turtles. Currently I have a turtle (trtl) and a user input. Here is how I have it set up
user_inp = []
x = input(" ")
user_inp.append(x)
I am trying to have it read through the code as if you'd read a book and not just search for 1 variable at a time. currently I have a def function set as
def move():
trtl.forward(20)
for w in user_inp():
if w in user_inp:
move()
The problem with that is that is say I put in "w w" as my input. It doesn't work and instead it shows up as nothing. I need help quite badly and when I tried explaining what I needed help on to my teacher he didn't understand. If any of you do and can help me please do so.
I'm assuming user_inp.ammend(x) is a typo. Obviously user_inp.append(x) is what you want.
Your logic is going to cause the turtle to always move 20 steps for each and every input character. The following code assigns the new variable w to each successive character in the user input. It's not getting all 'w' characters from their input:
for w in user_inp():
Similarly, now that w contains some character from the input, this code will always return true:
if w in user_inp:
That's because we already got the character out of the input, so it's guaranteed to be in the input.
Instead, you just have to do something like:
for letter in user_inp():
if letter == 'w':
move()
you need to change user_inp.ammend(x) to user_inp.append(x) and when you are looping through the user_inp list you don't need the parentheses because it isn't a function it is a list so that will send an error as well
I'm having some issues that I really would like some help on. Right now I'm trying to get the user to input a phone that only accepts the first three things typed in as numbers. The rest can be letters that will get converted into numbers, but even if the first three things are numbers or letters it will keep repeating. The input has to be in this format (XXX-XXX-XXXX).
examples: (234-SDF-SDFL) this would pass, if (SDF-FSD-UEIE) then the program would ask again until the first three are number. Please and thank you.
phNumber=input("Number: ")
num=phNumber.split("-")
area=num[0]
while area.isdigit() == False :
phNumber=input("Number: ")
num=phNumber.split("-")
area=num[0]
Why dont you use regular expressions? (BTW: Do not use this odd naming, use pythons naming convention: snake case)
import re
phone_number=input("Number: ")
while not re.match(r'^\d{3}-[A-Z]{3}-[A-Z]{4}$', phone_number):
phone_number=input("Number: ")
I'm new to programming and I need some help for a ai robot I just started on
Here is my code:
complements = "nice" and "happy" and "good" and "smart" and "wonderful"
var = "You are a "+ complements
input = raw_input
if var in input:
print "Thank you!"
else:
print "Wuhhhhh?"
If I type in something other than "nice" it goes to the else statement.
Or statements don't work
First, the and keyword does not do what you want. It is used as a binary comparison. Due to the inner workings of Python, your variable complements will receive the value "wonderful". You want to put these words in a list (see here). You will then be able to manipulate these words using concatenation as such (for example):
var = "You are a " + ", ".join(complements)
Furthermore, raw_input is a function. It must be called as such: raw_input(). Otherwise, you just create an alias of the function which you named input. You would still have to call it by appending () to it in order to receive the user input.
I also don't understand your if var in input: statement. var is a sentence you made, why would you search it in the user input? It would be clearer to do if raw_input() in complements, or something along the lines of it.
If you are beginning to learn Python, I would recommend you to use Python 3 instead of Python 2. raw_input() was renamed input() in Python 3.
For my program I ask the user to input a command. If the user writes: Input filename (filename being any possible name of a file in the computer) I want my program to only read Input so it knows what if statement to use and then open the file that the user wrote.
There is also another part where I have to do a similar task, where the user inputs: score n goals.(n is the top number of players the program has to read from a list) I want the program to differentiate this from 2 other similar tasks (score n misses and score n passes).
I am not sure if I'm approaching this the correct way, but this was my try for the first case I talked about, but it doesn't work.
user_input = input ('File name:')
input_lowered = user_input.lower()
command = input_lowered[0:4]
if command == 'input' :
fp = open ("soccer.part.txt")
else :
user_upper = input ('Input name:')
Thanks in advance for any clue of how I should aim at fixing this!!!
You can do that as follows:
your_string[0:5]
This will get the first five characters of the string as a string.
If you would like to grab a part of the string from the start then you can use:
my_string[:number]
if you would like to grab a part of the string from the end:
my_string[-number:]