Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Hey i don't know how to explain this so i will give an example.
print("hey eating # is a good idea")
What i want to happen is that when you type something. (answer = raw_import)
The # is what you typed.
Like if you typed orange. It would say ("hey eating orange is a good idea")
Also i want this to be after an else: statement.
I hope you understand my question.
This could be used for games, and other programs too. I bet many people have the same question and don't know what to google! Thanks!
Check this out. http://www.python-course.eu/python3_formatted_output.php
fruit = "orange"
say = "hey eating {0} is a good idea".format(fruit)
print(say)
Use str.format
say = "hey eating {} is a good idea"
if some_condition:
# do something
else:
a = raw_input("Enter your favourite fruit!")
print s.format(a)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm currently trying to perform this, but my book for class doesn't show anything in relation to Boolean variables or anything related to this problem. I've been looking online for a while and everything is way too complicated for something that should be simple according the unit. If the solution is posted here, what was typed in to find it? Maybe I need to rephrase my question.
https://i.stack.imgur.com/RMSEx.png
def getSecondWord(sentence):
res = sentence.split(' ')[1]
if res.endswith('.'):
return res[:-1]
else:
return res
inputSentence = 'broccoli is delicious'
secondWord = getSecondWord(inputSentence)
print("second word is:", secondWord)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm currently scraping some tables on the internet where numbers are posted in varying numerical formats:
Animal - Left in Wild
Tigers - 18
Deer - 18m
Pigs - 180000
I've managed to strip the m away from the number, but I am wondering if/how I could use a if statement to allow some manipulation to ensure I document the number accurately:
if animal.strip("m") == animal.strip("m"):
left_in_wild = left_in_wild * 1000000
Obviously that code does not work, but it is a rough thought of how I'm thinking about getting around this. If anyone can has anything they think can be helpful let me know.
Thank you!
A simple IF statement could help with what you're looking for:
animal = "18m"
if 'm' in animal:
print animal.strip('m') + ",000,000"
if 'k' in animal:
print animal.strip('k') + ",000"
returns:
18,000,000
Something like:
import re
def get_number(s):
try:
i=int(re.match('(\d+)', s).group(1))
if "m" in s:
i*=1000000
return i
except:
print "No Number"
get_numbers("18m") returns 18000000
You could even expand it to have an elif "k" in s block if you had thousands or something.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
More detail: program gives 3 answers and user has to pick one answer either you get it right and move on or wrong and you have to choose again. That's what I'm looking for after guessing/choosing wrong I wanna be able to choose again without program ending or giving me an error?
This might help you
while(True):
print "Enter Choice"
print "1).Correct"
print "2).Incorrect"
print "3).Incorrect"
choice = raw_input()
if(choice == '1'):
print "That's Correct"
break
else:
print "Please Try Again"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Ideally, I'd like the program to erase/un-log any and all erroneous character(s) when the Backspace key is pressed and replace them with the correct characters.
After searching for solutions to no avail, I'm wondering if it's even possible? If it is, my guess is that the code needed to do this might involve the modules: 're', 'readchar', 'msvcrt', 'getch' or some combination of those, in addition to using 'string.replace', 'x.remove', 'r/R', 'raw_input' 'x.translate', or the like. But I don't have the knowledge or skills yet to figure out how to apply them.
This code may be what you are looking for:
import re
text = "Helll[Back Space]o how are yoo[Back Space]u"
result = list(text)
for (start, end) in [(m.start(), m.end()) for m in re.finditer('\[Back Space\]', text)]:
text = text.replace(''.join(result[start-1:end]), '')
print text
Output:
Hello how are you
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In python, I understand it's possible to convert many types of data to a string using str(). Is there any way to reverse this? Let me show you what I mean.
exampleDictionary = {'thing on ground': 'backpack'}
backpack = {'tea': 'Earl Grey'}
def openBackpack():
#code to grab backpack from exampleDictionary
#code to convert 'backpack' to backpack
#code to access backpack and look at the tea
This is oversimplified of my code in progress, but it should be easy to see basically where I'm stuck at. If it's not clear I'm happy to clarify further.
This is a very weird way to handle data.
I would use a nested dicts:
exampleDictionary = {'thing on ground': {'backpack': {'tea': 'Earl Grey'}}}
print exampleDictionary['thing on ground']
print exampleDictionary['thing on ground']['backpack']
print exampleDictionary['thing on ground']['backpack']['tea']
Outputs:
{'backpack': {'tea': 'Earl Grey'}}
{'tea': 'Earl Grey'}
Earl Grey
You're looking for globals().
def openBackpack():
backpack= globals()[exampleDictionary['thing on ground']]
print(backpack['tea'])
This approach looks up the string for and already existing object in the code.
exampleDictionary = {'thing on ground': 'backpack'}
backpack = {'tea': 'Earl Grey'}
print( eval(exampleDictionary['thing on ground']) )
edit
to Adam Smith 's point eval isn't a safe and you shouldn't do this
... but it can be done and here is how.
eval https://docs.python.org/2/library/functions.html#eval
takes a string and interprets it as code.
x = eval("1+2")
x = 3
if you eval user input the likelyhood you know all of what a user can input or 'inject' into your code is unknown.