I prompt the user to enter a tuple but raw_input converts it to a string. How do I convert it back to a tuple as this is very annoying?
from sympy import *
plot(y , tuple)
The tuple is prompted by the user through raw_input
Related
I'm trying to execute the following code in Spyder 3.3.1 using Python 2.7.15. I'm a beginner.
text = str(input("You are lost in forest..."))
while text == "Right":
text = str(input("You are lost in forest..."))
print "You got out of the forest!!!"
When I run the code with integer value it works. For example the following piece of code:
text = input("You are lost in forest...")
while text == 1:
text = input("You are lost in forest...")
print "You got out of the forest!!!"
How can I make the input() work with a string value? Thank you for your help.
Use raw_input() instead of input():
value = raw_input("You are lost in forest...") # String input
value = int(raw_input("You are lost in forest...")) # Integer input
...
In python 2, raw_input() takes exactly what the user typed and passes it back as a string. input() tries to understand the data entered by the user.Hence expects a syntactically correct python statement.That's why you got an error when you enter Right. So you can enter "Right" as input to fix this error.
But it is better to use raw_input() instead of input().
I am trying to make a program which includes taking input from user and using that input to delay the program but I can't figure out how to use that input because Time.sleep() take only int or float as arguments but I need a function which delays the program like time.sleep() but take string as an arguments.
Here is the program:Now the input given by user is stored in t variable so I need some function which can take t as an argument.
import webbrowser
import time
print "This is the take a break program"
t= raw_input("In how much time will you take a break? \n")
print ("%s,seconds"%t)
time.sleep(t)
webbrowser.open("youtube.com")
You can convert a string to int/float in python, simply do:
#Let say this is your String
String mystring = "100"
#This will convert your string to int
int time = int(mystring)
#And this will convert it to a float
float timeFloat = float(mystring)
Remember to check your user input as it won't work as expected if the string is composed of letters only.
How to make the program choose a value from 1 to 100? The program cannot print the correct value
while True:
regage = raw_input('Enter your age:')
if regage not in range(1,100):
print 'Please put an apropriate number for your age'
else:
print 'you are', regage,'years old'
This should work. The problem was that raw_input returns a string, so the value should be converted to an integer before you can check if it's in the array of integers that range returns.
while True:
regage = raw_input('Enter your age:')
if int(regage) not in range(1,101):
print 'Please put an apropriate number for your age'
else:
print 'you are', regage,'years old'
Also, I changed the range to include 100.
i am very new to coding, and am in need of some assistance, and would like to say sorry for such a novice question, but I could not word the question in a way to easily find help, which i am sure is out there. Anyways, to put it simply, I need to force the user when asked to input text, to have the format 'a=b'.
`message3 = raw_input("Enter a guess in the form a=b:")`
I would like to make it so that if the user does not enter the correct format, 'a=b', some error message will pop up telling them so.
Here's how I would do it, expanding on my comment above. Since you're learning python, it's best if you learn python 3.
import sys
import re
s = input("Enter a guess in the form a=b:")
matched = re.match(r'(.+)=(.+)', s)
if matched is None:
print('enter in the form of a=b')
sys.exit(1)
a, b = matched.groups()
print(a, b)
In the regex, .+ matches a non-empty string. The brackets are a capturing group, so we can get a and b using .groups().
You can try this:
>>> import re
>>> string = raw_input("Enter a guess in the form a=b:")
Enter a guess in the form a=b: Delirious= # Incorrect Format
>>> m = re.match(r'.+=.+', string)
>>> try:
if m.group():
print "Correct Format"
except:
print "The format isn't correct"
"The format isn't correct"
>>>
>>> string = raw_input("Enter a guess in the form a=b:")
Enter a guess in the form a=b: Me=Delirious # Correct Format
>>> m = re.match(r'.+=.+', string)
>>> try:
if m.group():
print "Correct Format"
except:
print "The format isn't correct"
"Correct Format"
I have just started learning python 2.7, and as every new learner i am still getting accustomed to the syntax that python uses. I tried to write this code:
name = raw_input('What is your name?\n')
print 'Hi, %s.' % (name)
I guess the output for the above program should be:
Hi, What is your name?
But i am getting the output as:
What is your name?
After pressing enter key, i get another output:
Hi, .
What is the problem with my code?
There is no problem, it does exactly what you tell it to.
raw_input prints the string argument (as a prompt), and reads input from the user until the first newline, returning the text it has read. This is exactly what happens (try typing some text before hitting Enter). Then the second line takes that text and puts it into the formatting string, printing the result.
The raw_input function is used to take input form the prompt. The string you passed in function will be printed on the prompt followed by your input which gets completed (in sense of python) with the pressing of 'enter key'.
After that the next line print 'Hi, %s.' % (name) will get printed showing the name entered by user in the prompt, which, I assume, is None in your case as you are pressing enter key without any input.