why blank input print if statement in python - python-3.x

s1 = input("enter 1st string: ")
s2 = input("enter 2nd string: ")
if s2 in s1:
print("found")
else:
print("not found")
simply i want to say if i give some input to s1 and not give anything as input to s2 and press enter, Output of this code print "found"
which is wrong because blank is not in my string s1, So why it is happening? and how to correct it?

Every string contains the empty string so '' in 'anything' is always True.

Every string always contains the empty string.
i = 'anything'.count('')
print(i)
# 9
As you can see, between every letter and before and after the string, there is 'the empty string'.
In your example, you can ensure that a non-empty string is entered like so:
while(True):
s1 = input("enter 1st string: ")
if s1 != '':
break
while(True):
s2 = input("enter 2nd string: ")
if s2 != '':
break
if s2 in s1:
print("found")
else:
print("not found")
Also note:
s = '' + 'a' + '' + 'n'+ '' + 'y' + '' + 't' + '' + 'h' + '' + 'i' + '' + 'n' + '' + 'g' + ''
print(s == 'anything')
# True
print(s.count('')
# 9
Adding more empty strings does not have any effect.
s += ''
print(s.count('')
# 9

Related

My for loop is not iterating through all possible values

I'm trying to store substrings of letters in 's' that are in alphabetical order in a list
s = 'azcbobobegghakl'
string = ''
List = []
i = -1
for letter in s:
if letter == s[0]:
string += letter
elif letter >= s[i]:
string += letter
elif letter < s[i]:
List.append(string)
string = letter
i += 1
print(List)
My expected result:
['az', 'c', 'bo', 'bo', 'beggh', 'akl']
And my actual Output:
['az', 'c', 'bo', 'bo']
Firstly, your first if statement is incorrect. It should be if i == -1:. Because of this bug, you are ignoring the second a character in s.
Secondly, at the end of the string you don't add what's left of string into List.
As such, the following is what you want:
s = 'azcbobobegghakl'
string = ''
List = []
i = -1
for letter in s:
if i == -1:
string += letter
elif letter >= s[i]:
string += letter
elif letter < s[i]:
List.append(string)
string = letter
i += 1
List.append(string)
print(List)
An example is available here.

Caesar Cipher shift on new lines

I am having a problem with my code trying to do an advanced caesar cipher shift. I changed a certain letter to another, and then added a string to certain parts of a file before encoding, but am having problems doing the shifting now. This is my code:
import string
import sys
count = 1
cont_cipher = "Y"
#User inputs text
while cont_cipher == "Y":
if count == 1:
file = str(input("Enter input file:" ""))
k = str(input("Enter shift amount: "))
purpose = str(input("Encode (E) or Decode (D) ?: "))
#Steps to encode the message, replace words/letter then shift
if purpose == "E":
my_file = open(file, "r")
file_contents = my_file.read()
#change all "e"s to "zw"s
for letter in file_contents:
if letter == "e":
file_contents = file_contents.replace(letter, "zw")
#add "hokie" to beginning, middle, and end of each line
lines = file_contents.split('\n')
def middle_message(lines, position, word_to_insert):
lines = lines[:position] + word_to_insert + lines[position:]
return lines
new_lines = ["hokie" + middle_message(lines[x], len(lines[x])//2, "hokie") + "hokie" for x in range(len(lines))]
#math to do the actual encryption
def caesar(word, shifts):
word_list = list(new_lines)
result = []
s = 0
for c in word_list:
next_ord = ord(c) + s + 2
if next_ord > 122:
next_ord = 97
result.append(chr(next_ord))
s = (s + 1) % shifts
return "".join(result)
if __name__ == "__main__":
print(caesar(my_file, 5))
#close file and add to count
my_file.close()
count = count + 1
The error I am getting is:
TypeError: ord() expected a character, but string of length 58 found
I know that I need to split it into individual characters, but am not sure how to do this. I need to use the updated message and not the original file in this step...

python function not returning the full name, it returns "Q Filio!" why is that?

def get_formatted_name(first_name, last_name):
"""---"""
full_name = first_name + ' ' + last_name
return full_name.title()
while True:
print('\nPlease tell me your name:')
print("(enter 'q' at any time to quit)")
f_name = input('First name: ')
if f_name == 'q':
break
l_name = input('Last name : ')
if l_name == 'q':
break
formatted_name = get_formatted_name(f_name, l_name)
print("\nHello, " + formatted_name + '!')
The program will keep asking the user for his name until he inputs 'q' either for the first name or for the last name. Then it will overwrite that name with 'q' and treat that 'q' as part of the name instead of the previously entered name.
You should move the last two lines inside the while:
def get_formatted_name(first_name, last_name):
"""---"""
full_name = first_name + ' ' + last_name
return full_name.title()
while True:
print('\nPlease tell me your name:')
print("(enter 'q' at any time to quit)")
f_name = input('First name: ')
if f_name == 'q':
break
l_name = input('Last name : ')
if l_name == 'q':
break
formatted_name = get_formatted_name(f_name, l_name)
print("\nHello, " + formatted_name + '!')
Then the output is as expected:
Please tell me your name:
(enter 'q' at any time to quit)
First name: a
Last name : b
Hello, A B!
Please tell me your name:
(enter 'q' at any time to quit)
First name: c
Last name : d
Hello, C D!
Please tell me your name:
(enter 'q' at any time to quit)
First name: q
Process finished with exit code 0

Duplicate word in hangman game Python

I have a problem, when in Hangman game there is a word like happy, it only append 1 'p' in the list...run my code and please tell me what to do?
check my loops.
import random
import time
File=open("Dict.txt",'r')
Data = File.read()
Word = Data.split("\n")
A = random.randint(0,len(Word)-1)
Dict = Word[A]
print(Dict)
Dash = []
print("\n\n\t\t\t","_ "*len(Dict),"\n\n")
i = 0
while i < len(Dict):
letter = str(input("\n\nEnter an alphabet: "))
if letter == "" or letter not in 'abcdefghijklmnopqrstuvwxyz' or len(letter) != 1:
print("\n\n\t\tPlease Enter Some valid thing\n\n")
time.sleep(2)
i = i - 1
if letter in Dict:
Dash.append(letter)
else:
print("This is not in the word")
i = i - 1
for item in Dict:
if item in Dash:
print(item, end = " ")
else:
print("_", end = " ")
i = i + 1
The error is with the "break" on Line 25: once you have filled in one space with the letter "p", the loop breaks and will not fill in the second space with "p".
You need to have a flag variable to remember whether any space has been successfully filled in, like this:
success = False
for c in range(len(Dict)):
if x == Dict[c]:
Dash[c] = x
success = True
if not success:
Lives -= 1
P.S. There's something wrong with the indentation of the code you have posted.

How can I get eval() to execute a function I define? [Python]

I have some code that I need to evaluate in python 3.4.1
def foo(bar):
somecode
def myFunction(codeInString):
eval("foo(" + codeInString + ")")
But every time I try to evaluate it, I get an error that says
NameError: name 'foo' is not
When I use 'q Patrick m 1880 1885 2' as input, it should give me back a list of three tuples. Instead I get
File "C:\...\Project 1\names.py", line XXX, in BBNInput
return(eval("getBirthsByName(" + query + ")"))
File "<string>", line 1, in <module>
NameError: name 'getBirthsByName' is not defined
I defined tiny_names.csv below the code block
import matplotlib.pyplot as plt
import string
def names( infile = 'tiny_names.csv'):
#This takes in the name of the file containing the clean name data and returns a list in the format [year, name, sex, quantity]
def readNames(infile = 'tiny_names.csv'):
#Counts the number of name-year combinations
counter = 0
#Opens the file
file = open(infile, 'r')
#Reads each line into an item on the list
names = [i for i in file]
#Formatting
for i in names:
#increments the counter
counter = counter + 1
index = names.index(i)
#Removes the newline char
names[index] = i.strip('\n')
#splits by the comma
names[index] = names[index].split(',')
#reformats the numbers into ints, makes names all lowercase to make searching easier
names[index] = [ int(names[index][0]), names[index][1].lower(), names[index][2], int(names[index][3])]
#closes the file
file.close()
#returns the names
return(names)
#Takes names as an argument and returns a dictionary with keys of names and values of lists of tuples (year, male births, female births)
def nameIndex(names):
#Initialize the empty dictionary
result = {}
for i in names:
name = i[1]
if name not in result.keys():
#if it's a male, add in the male format
if i[2] == 'M':
result[name] = [(i[0], i[3], 0)]
#otherwise, add in the female format
else:
result[name] = [(i[0], 0, i[3])]
#Checking if there is already a datapoint for that year
elif True in [( i[0] == a[0]) for a in result[name]]:
xx = [( i[0] == a[0]) for a in result[name]]
index = xx.index(True)
#If there is a datum in the male slot, add the new datum to the female slot
if result[name][index][1] == 0:
result[name][index] = (result[name][index][0], i[3], result[name][index][2])
#Otherwise, vice versa
else:
result[name][index][2] == (result[name][index][0], result[name][index][1], i[3])
#if the name exists but the year is unique
else:
#if it is a male, add it in the male format
if i[2] == 'M':
result[name].append((i[0], i[3], 0))
#otherwise add it in the female format
else:
result[name].append((i[0], 0, i[3]))
#Return the results
return(result)
def getBirthsByName (name , gender=None, start=None, end=None, interval=None):
#initialize the return variable
thing = []
#Make the name variable auto match the format of the names
name = name.lower()
#if the name doesn't exist, say so
if name not in nameIndex:
return("Name not in index")
#if there are no time constraints
if not start and not end and not interval:
#cycle through the name Data Points (dp) and addup the numbers for
for dp in nameIndex[name]:
year = dp[0]
#Gender neutral
if not gender:
thing.append((year, dp[1] +dp[2]))
#Males
elif gender.upper() == "M":
thing.append((year, dp[1]))
#Females
elif gender.upper() == "F":
thing.append((year, dp[2]))
#Data validation, gender != m or f
else:
return("You have entered and invalid gender, because we are insensitive people")
else:
#Data Validation, see return comments
if interval and (not start or not end):
return("You must have a start and an end to have an interval")
if not end:
end = 2013 #initializes end if blank
if start:
return("You must have a start to have an end")
if not start:
start = 1880 #initializes start if blank
if not interval:
interval = 1 #initializes interval if blank
#If the input passes all the validation tests, return data
for year in range (start, end, interval):
if year not in yearIndex.keys():
continue
if name not in yearIndex.get(year).keys():
continue
#Assign the tuple to dp
dp = yearIndex.get(year).get(name)
#Gender neutral
if not gender:
thing.append((year, dp[0] +dp[1]))
#Males
elif gender.upper() == "M":
thing.append((year, dp[0]))
#Females
elif gender.upper() == "F":
thing.append((year, dp[1]))
return(thing)
def BBNInput(inp):
split = inp[2:].split()
query = '"' + split[0] + '"'
if (split[1] != None):
query = query + ',"' + split[1] + '"'
if (split[2] != None):
query = query + "," + split[2]
if (split[3] != None):
query = query + "," + split[3]
if (split[4] != None):
query = query + "," + split[4]
return(eval("getBirthsByName(" + query + ")"))
#read the names
print("read the names")
nameList = readNames()
#store the name index
print("make the name index")
nameIndex = nameIndex(nameList)
#initialize the exit bool
exit = False
#Functional loop
while not exit:
queue = []
inp = input('names> ')
if inp == 'x':
exit = True
continue
elif inp[0] == 'c':
data = BBNInput(inp)
total = 0
for dp in data:
total = total + dp[1]
print(total)
elif inp[0] == 'q':
print(BBNInput(inp))
elif inp[0] == 'p':
split = inp[2:].split()
query = '"' + split[0] + '"'
if (split[1] != None):
query = query + ',"' + split[1] + '"'
if (split[2] != None):
query = query + "," + split[2]
if (split[3] != None):
query = query + "," + split[3]
if (split[4] != None):
query = query + "," + split[4]
exec("print(getBirthsByName(" +query + "))")
names()
tiny_names.csv =
1890,Patrick,M,227
1890,Mary,F,12078
1890,Charles,M,4061
1890,Alice,F,2271
1889,Patrick,M,236
1889,Mary,F,11648
1889,Charles,M,4199
1889,Alice,F,2145
1888,Patrick,M,245
1888,Mary,F,11754
1888,Charles,M,4591
1888,Alice,F,2202
1887,Patrick,M,218
1887,Mary,F,9888
1887,Charles,M,4031
1887,Alice,F,1819
1886,Patrick,M,249
1886,Mary,F,9890
1886,Charles,M,4533
1886,Alice,F,1811
1885,Patrick,M,217
1885,Mary,F,9128
1885,Charles,M,4599
1885,Alice,F,1681
1884,Patrick,M,222
1884,Mary,F,9217
1884,Charles,M,4802
1884,Alice,F,1732
1883,Patrick,M,213
1883,Mary,F,8012
1883,Charles,M,4826
1883,Alice,F,1488
1882,Patrick,M,249
1882,Mary,F,8148
1882,Charles,M,5092
1882,Alice,F,1542
1881,Patrick,M,188
1881,Mary,F,6919
1881,Charles,M,4636
1881,Alice,F,1308
1880,Patrick,M,248
1880,Mary,F,7065
1880,Charles,M,5348
1880,Alice,F,1414
Well, now I think there is too much code to obviously see what the problem is. However, you probably don't need to even use eval() at all. The following will likely do what you want:
def BBNInput(inp):
split = inp[2:].split()
return getBirthsByName(*split)
See What does ** (double star) and * (star) do for Python parameters? for further information on what this means.
I fixed it by changing my BBNInput function to a query building function and evaluated (using eval()) on the same level as the def for getBirthsByName. But I think that #GregHewgill has the right of it, makes way more sense. Thanks for your help everyone!

Resources