Very new to Python and need guidance - python

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)

Related

Add space before reading an input in python

I have a python program which works as a calculator.
import re
value = 0
run = True
while run:
if value == 0:
equation = input('Enter the equation ')
else:
equation=input(str(value))
# print(equation + ' ')
if equation == 'quit':
run = False
else:
equation = re.sub("[a-zA-Z""]",'',equation)
if value ==0:
# equation=eval(equation)
value=eval(equation)
else:
value=eval(str(value)+equation)
print(equation + ' ')
The program works without problem , but when reading the input after the first iteration , the terminal is as below
python3 file.py
Enter the equation 10+10
20+30
The next input can be provided next to 20. I want to add a space before reading the input , so the output will be as shown below
python3 file.py
Enter the equation 10+10
20 +30
How can I add a space before reading the next input so the cursor will create a space
Example
input=input('Enter the input ')
Python Version : 3.7.7
You may simply append a space to the string given to input.
In your example, changing the line equation=input(str(value)) to equation=input(str(value) + ' ') will produce your desired output.
IMHO, this line is not what you want:
equation=input(str(value))
Outputting the result of the calculation should be separate from the next input because of semantics. The argument to input() is the prompt to tell the user what to do. "20" (or whatever intermediate result) is not a good instruction for the user.
So the code becomes
print(value, end=" ") # end=" " prevents a newline and adds a space instead
equation=input() # don't give any prompt, if there's no need to

Python print and input on same line

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.

How to print a string backwards using a for loop

I have to create a program that lets the user enter a string and then, using range, the program outputs that same string backwards. I entered this into python, but it goes me an error, in the 4th line, it says 'int' object has no attribute '__getitem__'.
Can someone please help me correct it? (Using range)
user=raw_input("Please enter a string: ")
user1=len(user)-1
for user in range(user1,-1,-1):
user2=user[user1]
print user2
I think you have a mistake because you keep using the same words to describe very different data types. I would use a more descriptive naming scheme:
user = raw_input("Please enter a string: ")
user_length = len(user)
for string_index in range(user_length - 1, -1, -1):
character = user[string_index]
print(character)
For example, if the user input was foo, it would output:
o
o
f
You are overwriting the user string with your for-loop, fix it by changing the for-loop variable
Fix:
for ind in range(user1,-1,-1):
user2 = user[ind]
print (user2)
Alternative (without for-loop):
print user[::-1]
This is because you are overwriting the string user with an int in the line for user in range(...)
Perhaps you'd be better off with:
user=raw_input("Please enter a string: ")
for user1 in range(len(user)-1,-1,-1):
user2=user[user1]
print user2
your user has been overwrited in your for loop. Take this(Use range)
user=raw_input("Please enter a string: ")
print ''.join([user[i] for i in range(len(user)-1, -1, -1)])
Python 3 solution:
user=input("Please enter a string: ")
for ind in range(1,len(user)+1):
char = user[-ind]
print(char)
And another non for loop solution is:
''.join(reversed(user))

Restricting the User Input to Alphabets

I'm a technical writer learning python. I wanted to write a program for validating the Name field input,as a practise, restricting the the user entries to alphabets.I saw a similar code for validating number (Age)field here, and adopted it for alphabets as below:
import string
import re
r = re.compile(r'[a-zA-Z]+')
print "WELCOME FOR NAME VERIFICATION. TYPE ALPHABETS ONLY!"
print raw_input("Your Name:")
x = r
if x == r:
print x
elif x != r:
print "Come on,'", x,"' can't be your name"
print raw_input("Your Name:")
if 5<=len(x)<=10:
print "Hi,", x, "!"
elif len(x)>10:
print "Mmm,Your name is too long!"
elif len(x)<5:
print "Alas, your name is too short!"
raw_input("Press 'Enter' to exit!")
I intend this code block to do two things. Namely, display the input prompt until the user inputs alphabets only as 'Name'. Then, if that happens, process the length of that input and display messages as coded. But, I get two problems that I could not solve even after a lot of attempts. Either, even the correct entries are rejected by exception code or wrong entries are also accepted and their length is processed.
Please help me to debug my code. And, is it possible to do it without using the reg exp?
If you're using Python, you don't need regular expressions for this--there are included libraries which include functions which might help you. From this page on String methods, you can call isalpha():
Return true if all characters in the string are alphabetic and there is at least one character, false otherwise.
I would suggest using isalpha() in your if-statement instead of x==r.
I don't understand what you're trying to do with
x = r
if x == r:
etc
That condition will obviously always be true.
With your current code you were never saving the input, just printing it straight out.
You also had no loop, it would only ask for the name twice, even if it was wrong both times it would continue.
I think what you tried to do is this:
import string
import re
r = re.compile(r'[a-zA-Z]+')
print "WELCOME FOR NAME VERIFICATION. TYPE ALPHABETS ONLY!"
x = raw_input("Your Name:")
while not r.match(x):
print "Come on,'", x,"' can't be your name"
x = raw_input("Your Name:")
if 5<=len(x)<=10:
print "Hi,", x, "!"
elif len(x)>10:
print "Mmm,Your name is too long!"
elif len(x)<5:
print "Alas, your name is too short!"
raw_input("Press 'Enter' to exit!")
Also, I would not use regex for this, try
while not x.isalpha():
One way to do this would be to do the following:
namefield = raw_input("Your Name: ")
if not namefield.isalpha():
print "Please use only alpha charactors"
elif not 4<=len(namefield)<=10:
print "Name must be more than 4 characters and less than 10"
else:
print "hello" + namefield
isalpha will check to see if the whole string is only alpha characters. If it is, it will return True.

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