Why I am getting a duplicate output in Python? - python-3.x

I am trying the below code. But in the final output, I am getting repeated words. For example, if I input name as Jai, I will get JaiJai.
name = input ("Cheer: ")
for i in name:
name +=i
print('Give me a', i+",", i+"!")
print("What does it spell?")
print(name)

Because of this:
for i in name:
name +=i
for every character in a given word, add this character to the word.

You adding up the value of i to the name variable
this line name +=i is redundant here :)
corrected code:
name = input ("Cheer: ")
for i in name:
print('Give me a', i+",", i+"!")
print("What does it spell?")
print(name)

Related

Find duplicate letters in two different strings

My program asks the user to input first name then last name. How can I make it find the duplicate letters from first and last name? and if there is none it will print "no duplicate value in First and Last name"
here is my code but i need the output to be "the duplicate characters... is/are ['a', 'b', 'c']". and also when there is no duplicate my code prints multiple "No duplicate value in First name and Last name" but i need it to be one only.
for fl in firstName:
if fl in lastName:
print("The duplicate character in your First name and Last name is/are: ", tuple(fl))
else:
print("No duplicate value in First name and Last name")
One way to do so,
first_name = 'john'
last_name = 'Doe'
for letter in first_name:
if letter in last_name:
print(letter)

How do I check user input from a file in Python?

Let's say I have a text file:
Alice,Bob,Charles,David,Emily,Frank
I want to check if a string that the user enters matches a name in the text file, something like this:
Name = str(input("Enter a name here: ")
if Name == Alice:
print("Foo")
elif Name == Bob:
print("Bar")
How do I do it?
Assuming that you want to check if a string that the user enters matches a name in the text file then you should read your text file in advance and then compare to the user input as follows
names = [i.split(',') for i in open('names.txt', 'r')][0]
Name = input()
[name+' is in the list' for name in names if name == Name]
Assuming that you want to check if a string that the user enters matches a name in the name of a text file then you should read your text file names in advance and then compare to the user input as follows:
names = os.listdir(directory)
Name = input()
[name+' is in the list' for name in names if name == Name]
lets assume that your directory looks something like this :
$cd folder
$ls
Alice Bob Charles David Emily Frank
Then you can do it by.
Name = str(input("Enter a name here: ")
if Name in os.listdir('folder'):
print("Name found!!!! {}".format(Name))

i want to delete the element even if user input is in small or Capital letter

nametoRemove = input("Enter the name you want to remove:")
print(nametoRemove)
name = ["Ramesh","Rakesh","Suresh"]
name.remove(nametoRemove)
print(name)
you can do it fith basic for loop and if statement:
nametoRemove = input("Enter the name you want to remove:")
print(nametoRemove)
name = ["Ramesh","Rakesh","Suresh"]
for item in name:
if item.lower() == nametoRemove.lower():
name.remove(item)
print(name)
output:
Enter the name you want to remove:ramesh
ramesh
['Rakesh', 'Suresh']

What did I do wrong

import sys
super_heroes = {'Iron Man' : 'Tony Stark',
'Superman' : 'Clark Kent',
'Batman' : 'Bruce Wayne',
}
print ('Who is your favorite Superhero?')
name = sys.stdin.readline()
print ('Do you know that his real name is', super_heroes.get(name))
I'm doing a simple code here that should read an input in a dictionary and print it out after a string of letters, but when ran it prints out
"
Who is your favorite Superhero?
Iron Man
Do you know that his real name is None
"
Even Though the input is in my dictionary.
Your input is having a newline at the end of the line.
I have tried it online REPL. Check it
try following to resolve it.
name = sys.stdin.readline().strip()
After stripping Check here
sys.stdin.readline() returns the input value including the newline character, which is not what you expect. You should replace sys.stdin.readline() with input() or raw_input(), which are really more pythonic ways to get input values from the user, without including the newline character.
raw_input() is preferable to ensure that the returned value is of string type.
To go a little bit further, you can then add a test if name in super_heroes: to perform specific actions when the favorite superhero name is not in your dictionary (instead of printing None). Here is an example:
super_heroes = {'Iron Man' : 'Tony Stark',
'Superman' : 'Clark Kent',
'Batman' : 'Bruce Wayne',
}
print ('Who is your favorite Superhero?')
name = raw_input()
if name in super_heroes:
print ('Do you know that his real name is', super_heroes[name], '?')
else:
print ('I do not know this superhero...')
sys.std.readline() appends a line break at the end of user input you may want to replace it before getting your Super Hero:
name = name.replace('\n','')

How do i display an input in Python within a print statement

name = input('What is You name ')
this shows me only the input, I need to display 'hello' as well!!
print ('Hello', input(name))
When I try to type this :-
name = input('What is You name ')
print ('Hello')
input(name)
Why is that it displays the name directly and not the hello keyword. Could anyone please update on the same and share a coed that might be helpful.
This is even the exact example in the docs
person = input('Enter your name: ')
print('Hello', person)

Resources