This question already has answers here:
How to read keyboard input?
(5 answers)
Closed 6 years ago.
I am working on a python project and I have got the following questions.
How to catch a character in python ? What modules I need to use ? What functions I need to use?
If you need to take a line of input just use:
x = input('Your name: ')
y = input()
Or (For Python 2)
x = raw_input('Your name: ')
y = raw_input()
For taking just one character from the keyboard you can use msvcrt.getch():
import msvcrt
key = msvcrt.getch()
if key == 'a':
print("You pressed a")
Related
This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
Is there a short contains function for lists?
(6 answers)
Closed 12 days ago.
I have this code:
import random;
print('Options:');
print('* Goblin');
choice = input('Your Choice:\n')
if choice == ('Goblin', 'goblin', 'GOBLIN'):
#generate the names
goblin_names = ('Xoict','Aag','Tiak','Hebrit','Brarvoikx','Glaarm','Kralx','Fubtard','Teesmoz','Streanierd','Brots','Prakx','Creard','Agsoq','Wissysz','Bluk','Cleaz','Ekiats','Gniatmex','Gledurx');
goblinNameGen = random.choice(goblin_names);
print("Name: "+ goblinNameGen);
print('');
else:
print("This creature has not been added yet. =(");
When I typed goblin, it should have entered the if block and displayed a name. Instead, it displayed the This creature has not been added yet. =( message from the else block.
Why does this happen?
This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 6 years ago.
Just a little project i'm working on to improve my knowledge.
Curious as to why the program always returns failure, even if the captcha is correctly entered. I assume it has something to do with the results not being stored in memory?
import string
import random
def captcha_gen(size=7, chars=string.ascii_letters + string.digits):
return ''.join(random.SystemRandom().choice(chars) for _ in range(size))
results = print(captcha_gen())
user_input = input("Please enter the captcha code as you see it: ")
if user_input == results:
print("success")
elif user_input != results:
print("failure")
else:
print("error")
Thanks!
results = print(captcha_gen())
print() returns None - it is used to print stuff to the screen. In this case, it is grabbing the output of captcha_gen() and printing it to the screen.
All functions in Python return something - if they don't specify what they return, then it is an implicit None
You want
results = captcha_gen()
This question already has answers here:
Reverse / invert a dictionary mapping
(32 answers)
Closed 6 years ago.
I have made my own Morse Code translator where you can enter the code and the corresponding letter prints out. However, what I want to do is that whenever I enter a letter, the code prints out. Here's my code:
MorseCode = {'.-':'A',
'-...':'B',
'-.-.':'C',
'-..':'D',
'.':'E',
'..-.':'F',
'--.':'G',
'....':'H',
'..':'I',
'.---':'J',
'-.-':'K',
'.-..':'L',
'--':'M',
'-.':'N',
'---':'O',
'.--.':'P',
'--.-':'Q',
'.-.':'R',
'...':'S',
'-':'T',
'..-':'U',
'...-':'V',
'.--':'W',
'-..-':'X',
'-.--':'Y',
'--..':'Z',
'.----':1,
'..---':2,
'...--':3,
'....-':4,
'.....':5,
'-....':6,
'--...':7,
'---..':8,
'----.':9,
'-----':0
}
print "Type 'help' for the morse code."
print "Type 'end' to exit the program.\n"
while True:
code = raw_input("Enter code:")
if code in MorseCode:
print MorseCode[code]
So the question is: Is there a way to somehow invert this dictionary so whenever I enter 'A', '.-' will print out? I'm only studying python for two weeks now so I'm still mastering the basics before I move on to the more advanced levels. Thank you!
You can use dictionary comprehension (assuming you are using Python 2.6+) to easily create a new, inverted dictionary:
letters_to_morse = {char: code for code, char in MorseCode.items()}
letters_to_morse['A']
>> '.-'
This question already has answers here:
TypeError: 'dict_keys' object does not support indexing
(5 answers)
Closed 7 years ago.
So,
I am using Python 3.4.2 and I have this code:
import csv
import random
filename = input("Please enter the name of your file: ")
# Open file and read rows 0 and 1 into dictionary.
capital_of = dict(csv.reader(open(filename)))
# Randomly select a country from the dictionary
choice = random.choice(capital_of.keys())
# The correct capital corresponds to the dictionary entry for the country
answer = capital_of[choice]
# Take a guess from the user
guess = input("What is the capital of %s? " % choice)
# If it's right, let the user know
if guess == answer:
print("Right-o!")
# Otherwise, do what you want to do.
This code was given to me as a solution on a previous question but upon entering the name of my CSV file, I get this error:
TypeError: 'dict_keys' object does not support indexing
Does anybody know a fix for this?
Thanks
Try this:
choice = random.choice(list(capital_of.keys()))
This question already has answers here:
Dynamic variable in Python [duplicate]
(2 answers)
Closed 8 years ago.
What im trying to do is have the user type a number, which would then register into the system as a specific variable.
Example:
n1 = "X"
n2 = "Y"
n3 = "Z"
num = input("enter a number (1-3): ")
print(n(num))
So, if the user entered the number 2 into their program, the program would display the value stored in n2, or be able to use n2 in an equasion.
Is this possible? I'm still new to Python and this is not a school assignment, just my own curiosity :)
Thanks
EDIT:
Here is what im trying to do:
temp = int(input("\nPlayer One, please pick a square (1-9): "))
while {1:n1, 2:n2, 3:n3, 4:n4, 5:n5, 6:n6, 7:n7, 8:n8, 9:n9}[temp] == "X" or {1:n1, 2:n2, 3:n3, 4:n4, 5:n5, 6:n6, 7:n7, 8:n8, 9:n9}[temp] == "O":
temp = str(input("\nPlayer One, please pick a valid square (1-9): "));
{1:n1, 2:n2, 3:n3, 4:n4, 5:n5, 6:n6, 7:n7, 8:n8, 9:n9}[temp] = "X"
You could use a dictionary for this. Like:
num = input("...")
print {1:n1, 2:n2, 3:n3}[num]
Hope that helps.