python idle how to create username? - python

Write a function called getUsername which takes two input parameters, firstname (string) and surname (string), and both returns and prints a username made up of the first character of the firstname and the first four characters of the surname. Assume that the given parameters always have at least four characters.

First, you will want to make a function. (Please note any syntax I use will be for V2.7)
def makeUsername(firstName,lastName):
Next, I would suggest a string to store the username that you will make. You only want the first character and then the first 4. Note the first and last names will be whatever you name the parameters.
x = firstName[0] + lastName[:4]
Finally, printing the string and returning the string.
print x
return x
Then, when you call the function it will look something like this:
makeUsername('John', 'Smith')
If you have any more questions, just ask!

This is how you build the string: firstname[0] + surname[:4]

Related

Creating a list of names base on user input()

I've been trying to find a way to transform users' name inputs into a python list, so I could later process the information. However, since I'm not very experienced, I can't find a way to do such a thing. When I write the code below, the print() output, is only the second letter of the first name, while I actually intend it to be the second name on the list. So, my question is, how can I make it so that each different name is a different element in the list?
# Creates a list of the players
players = input("Insert players\n")
list(players)
print(players[1])
Use split() to turn a string into a list of words:
players = input("Insert players\n").split()
print(players[1]) # prints the second player name in the list
By default split() splits on whitespace, but it takes an optional argument that allows you to split on other strings (e.g. split(",") to split on commas).

how to check last characters of a string

I'm trying to make a program that only accepts valid email addresses without using anything super fancy. I'm trying to use negative indexing to get the last characters of the string the user enters and make sure the input is valid. I can't seem to figure out how to check the last characters of the string using this method. Here's what I have so far:
email = 'None'
while email != '#gmail.com':
email = input("Please enter your email. It must be a valid Gmail email: ")
if '#gmail.com' in email[-11:0]:
continue
else:
print("Enter a valid Gmail email.")
I've tried rearranging the values in the index and changing the values themselves, but no matter what it always says to enter a valid email even if it does end in #gmail.com. I'm not trying to allow any valid email, I only care about Gmail emails so I need to work for this only.
str='abc#gmail.com'
sliced_str=str[-10:]
this gives a string with last 10 chars in string. But a better approach would be to use endswith() function like this:
if str.endswith("#gmail.com")
you also need to check if the user input has multiple #'s as well. SO, to consider both you can do something like this:
if str.count('#')==1 and str.endswith("#gmail.com")
To address the comments, you can create a simple function like this to check the mail address like this:
def check_mails(mail_address, dom_list):
for i in dom_list:
if mail_address.endswith(i):
return True
return False
and in your if condition :
if str.count('#')==1 and check_mails(str, ['#yahoo.com', '#gmail.com', '#hotmai.com'])
for checking if the user has intput only '#gmail.com' you can do that with the size of string like this: (considering an email has at least 3 characters before domain name)
if str.count('#')==1 and len(str)>=13 and str.endswith("#gmail.com")
You can use endswith:
if email.endswith("#gmail.com"):
If you want to stick with negative indexing, you need to get rid of the 0. Also, you only need the last 10 characters to match '#gmail.com'. This should work better: email[-10:].
In email[-11:0], the 0 after the colon makes it try to match all characters whose indices are greater than or equal to the length of the string minus 11, and also less than 0. There aren't any indices in that range, so it won't match anything.

Am I able to use the function 'count()' to find the amount of upper cases in a password? (PYTHON)

When I enter the code below, it says:
TypeError: must be str, not list
Does this mean I cannot use the function count() or is there another way I could program it?
password = "CheeseMakesMeHappy"
uppercase =["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"]
print (password.count(uppercase))
Just go through every character in the password and check if it is an uppercase character.
For example:
password = "FoOoObA"
print(len([c for c in password if c.isupper()]))
>> 4
Another method is using sets and bitmasks to count the number of unique uppercase characters.
password = "CheeseMakesMeHappy"
uppercase = set(["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"])
print(len(set(password)&uppercase))
>> 3
The set solution however will only count UNIQUE characters, but in the case of password strength metering that might not be a bad idea.
The problem is that the method count() expects a string object. Right now, with this line (password.count(uppercase)), you are effectively passing an Array object to your function. See zeraien's answer for a good solution.

If I have a variable in Python 2 with a string in it, how can I split this into several variables each with one character in?

The idea is, a user inputs an identification code, and, to check whether it meets a certain letter/number format, and that prohibited characters are not used in certain positions, each character is in its own variable.
number = input("What is your identification number?")
if first character is a letter:
append to list
else:
if first character number:
et cetera
First, in Python 2, input() does evaluation. To get a string, user should enter something in string syntax. E.g., input
"1234"
results in string "1234", but
1234
results in number 1234. It seems to me you need raw_input() instead. But you should notice Python3 doesn't evaluate, its input is equivalent to Python2 raw_input.
After this correction, you would get a string. If number is its variable name, number[0] is the very first character, number[1] is the second one, and so on.
I also suggest doing strip() on the string before analyzing it, because it's too typical for users to enter insignificant spaces before or after the real value.

Def function intro

I have to define a function named introduce(). It asks for the name of a person twice, and then introduce each of the two people to the other. The function introduce() takes one string parameter
for instance it would say:
What is your name?
John
And What is your name? Mike
it would then return:
John meet Mike,
Mike meet John
the code I have so far is
def introduce(intro):
1st = input('What is your name?: ')
2nd = input('And what is your name? ')
print(1st(input) 'meet' 2nd(input))
I would like to know what I am doing wrong, I am new to Python so I am not too familiar with some of the elements in it
Among other things, variable names in Python (and most other languages) can't start with digits, so use first and second.
You're also doing some weird syntactical stuff in your print call, which definitely doesn't work. Once you assign the two variables, just print them out--there's no need to refer to the input function again. Just pass first, second, and anything else as comma-separated arguments to the print function:
print(first, 'meet', second)
I highly recommend you work through some of the Python tutorials. This is all really basic syntactical stuff; doing the examples there will likely help you out a lot.
First, in Python, variable names cannot start with numerals, so try naming them first and second
Second, the (input) after the variables is unnecessary (and wrong). The variable name speaks for itself.
Third, separate your variables and 'meet' with a comma, so they're printed with a space between them and automatically joined together. There are other ways, but this is the simplest
def introduce(intro):
first = input('What is your name?: ')
second = input('And what is your name? ')
print(first, 'meet', second)
print(second, 'meet', first)
first of all, you need to not start variable names with numbers.
the input is saved in the variable 1st. You don't need to use 1st(input). Instead, you just `print(first)
Also, there is a problem with the way you join the strings. It can be done in two ways:
#Method 1:
print(1st, 'meet' , '2nd')
or
#Method 2:
print(1st + ' meet ' + 2nd)
the difference is: a comma will add the space, whereas a space is required when using + (at least, they are the only differences you need to be aware of.)

Categories