Python print and input on same line - python

I am trying to execute this but not able to.
Can someone assist?
teamname1 = print(input((plyer1,' Name of your team? '))
teamname2 = print(input(plyer2,' Name of your team? '))
print(teamname1)
print(teamname2)

Three issues:
the first line contains one parenthesis too many
input() takes only one argument, the prompt. If plyer1 is a
string, you must concatenate it
same as in comment: print() does not return anything, and can be
omitted because the prompt of the input() command is already
displayed.
You probably need something like this:
plyer1 = 'parach'
plyer2 = 'amk'
teamname1 = input(plyer1 + ' Name of your team? ')
teamname2 = input(plyer2 + ' Name of your team? ')
print(teamname1)
print(teamname2)

I'm not exactly sure what you're trying to do with the teamname variables. If you could modify the question/code it would help a lot.
As far as print and input on the same line, I think this might be what you're going for.
print("player1" + " " + input("Name of your team: "))
print("player2" + " " + input("name of your team: "))
P.S. There are many tutorials on-line that could help. Make sure to look around first, then come here.

Related

How to create a new line when only using \n with variables rather than printed text?

I am using Python for reference.
I am trying to create a new line when displaying name, address, city, state, and zip code. However, when I do this, IDLE tells me that "unexpected character after line continuation character". My code is as follows:
name = input('What is your name? ')
address = input('What is your street address? ')
city = input('What city do you live in? ')
state = input('What state do you live in? ')
zip_code = input('What is your zip code? ')
print(name\naddress)
I know that I can print each thing separately but I want to know how I can print the result using one print function. I know I can do it if I were to have simple text such as:
print('You have to\nshow up to class')
I am basically looking for the code to result as follows:
firstName lastName (first line)
address (second line)
city, state zip-code (third line)
Is there any way to input new lines before variables?
Any help would be appreciated!
I would do all my print statements using f-strings. It solves both the problems you stated
Example: Newline:
print(f"{name}\n{address}")
Example: No spaces:
print(f"{city},{state},{zip_code}")
Is this what you are looking for? The f-strings can be manipulated for many such variations of how you want your print output to look like.
UPDATE 1: With custom function or string concatenation
If you want to do this without using f-strings and conventional print function approach you can create your own custom print function. Here's just one of the many ways:
def prettyprint(*args):
for arg in args:
print(arg)
prettyprint(name, address)
Or you can add a new line while obtaining the input for you variables like so,
name = input('What is your name? ') + '\n'
print(name+address)
Or finally just combine it during print as,
print(name+'\n'+address)
Not sure if any of this is what you need but thought will provide a
few more options for you to explore.
You can use string formatting:
print("{}\n{}\n{}, {} {}".format(name, address, city, state, zip_code))
Here is your problem solution i have solved. If you want to use just one print function to print all over the statement you have to use f""#(f string). with \n and for spacing use \t.
name = input('What is your name? ')
address = input('What is your street address? ')
city = input('What city do you live in? ')
state = input('What state do you live in? ')
zip_code = input('What is your zip code? ')
print(f"Name:{name}\nAddress:{address}\nCity:{city}\nState:{state}\nZip code:{zip_code}\n")
print(f"{city}\t{state}\t{zip_code}")
output:
Name:Elon
Address:25 sampur
City:Washington D.C
State:Alaska
Zip code:99501
Washington D.C Alaska 99501
Sorry for random Address

im learning about files and excptions in the python crash course book. cant figure out why its saying votes = judge.read() has no attribute to write

print("\nEnter 'q' if you want to end poll.")
while True:
person = input('What is your name? ')
answers = input('what do you like most about programming? ')
print(person + ", your answer have been stored. Thanks for input.")
if person == 'q':
break
pollss = 'poll_record.txt'
with open(pollss,'r+') as judge:
votes = judge.read()
votes.write(person)
votes.write('\nAnswer was ' + answers)
votes.write('\n')
booth = ''
for counts in votes:
booth+=votes
im new to python so i understand if its a easier way to write this code but i will learn that later this how i understand how to write it now. but any construtive criticism will be helpful thank you.
Just change this three line
judge.write(person)
judge.write('\nAnswer was ' + answers)
judge.write('\n')
because you want to write in that file.
Your problem is because in votes = judge.read() you are assigning the content inside judge to variable votes, and it is interpreted as a string. votes is not a file, just a string, so it doesn't have the attributes that a file does, which is why when you try to write() to that string, it returns an error. You should instead write to the file:
with open(polls, 'r+') as judge:
judge.write(person)
judge.write('\nAnswer was', answers, '\n')
This should solve the problem.

Very new to Python and need guidance

interactive input prompt to open browsers...(will print something instead for now).
chrm = ['Google Chrome', 'Chrome']
input("type a browser..: ")
if chrm[0:1] == input():
print("starting: " + chrm)
What my intention is for this little thing is for a person to write one of the two possible input options..."Google Chrome" or "Chrome" to get a certain response. like openfile or printing something. but I can't seem to get it right.
You should assign the returning value of input() to a variable, and use the in operator to test if it is one of the values in the chrm list:
chrm = ['Google Chrome', 'Chrome']
i = input("type a browser..: ")
if i in chrm:
print("starting: " + i)

How to define a variable inside the print function?

I am a newbie in this field, and I am trying to solve a problem (not really sure if it is possible actually) where I want to print on the display some information plus some input from the user.
The following works fine:
>>> print (" Hello " + input("tellmeyourname: "))
tellmeyourname: dfsdf
Hello dfsdf
However if I want to assign user's input to a variable, I can't:
>>> print (" Hello ", name = input("tellmeyourname: "))
tellmeyourname: mike
Traceback (most recent call last):
File "<pyshell#47>", line 1, in <module>
print (" Hello ", name = input("tellmeyourname: "))
TypeError: 'name' is an invalid keyword argument for this function
I have researched inside here and other python documentation, tried with %s etc. to solve, without result. I don't want to use it in two lines (first assigning the variable name= input("tellmeyourname:") and then printing).
Is this possible?
Starting from Python 3.8, this will become possible using an assignment expression:
print("Your name is: " + (name := input("Tell me your name: ")))
print("Your name is still: " + name)
Though 'possible' is not the same as 'advisable'...
But in Python <3.8: you can't. Instead, separate your code into two statements:
name = input("Tell me your name: ")
print("Your name is: " + name)
If you often find yourself wanting to use two lines like this, you could make it into a function:
def input_and_print(question):
s = input("{} ".format(question))
print("You entered: {}".format(s))
input_and_print("What is your name?")
Additionally you could have the function return the input s.
no this is not possible. well except something like
x=input("tell me:");print("blah %s"%(x,));
but thats not really one line ... it just looks like it

New line for input in Python

I am very new to Python programming (15 minutes) I wanted to make a simple program that would take an input and then print it back out. This is how my code looks.
Number = raw_input("Enter a number")
print Number
How can I make it so a new line follows. I read about using \n but when I tried:
Number = raw_input("Enter a number")\n
print Number
It didn't work.
Put it inside of the quotes:
Number = raw_input("Enter a number\n")
\n is a control character, sort of like a key on the keyboard that you cannot press.
You could also use triple quotes and make a multi-line string:
Number = raw_input("""Enter a number
""")
If you want the input to be on its own line then you could also just
print "Enter a number"
Number = raw_input()
I do this:
print("What is your name?")
name = input("")
print("Hello" , name + "!")
So when I run it and type Bob the whole thing would look like:
What is your name?
Bob
Hello Bob!
# use the print function to ask the question:
print("What is your name?")
# assign the variable name to the input function. It will show in a new line.
your_name = input("")
# repeat for any other variables as needed
It will also work with: your_name = input("What is your name?\n")
in python 3:
#!/usr/bin/python3.7
'''
Read list of numbers and print it
'''
def enter_num():
i = input("Enter the numbers \n")
for a in range(len(i)):
print i[a]
if __name__ == "__main__":
enter_num()
In the python3 this is the following way to take input from user:
For the string:
s=input()
For the integer:
x=int(input())
Taking more than one integer value in the same line (like array):
a=list(map(int,input().split()))

Categories