Letting unbanned users commentopening a text file and building a dictionaryIterating and fetching values from dictionaryLoop an array of dictionary with keys containg sets; comparing each key, value pair; and combining dictionariesSettings JAVA_HOME with PowershellSimple Stack DemoHomogeneousDict - The Python dictionary that can have homogeneous keysRSA algorithm implementation in Python 3Search father (x) of the number D(x) and measure time and memory consumed

Is it possible to know the exact chord from the roman numerals

Installing Windows to flash UEFI/ BIOS, then reinstalling Ubuntu

What are those bumps on top of the Antonov-225?

How do I ask for 2-3 days per week remote work in a job interview?

Cases with long math equation

Doesn't the speed of light limit imply the same electron can be annihilated twice?

Did Pope Urban II issue the papal bull "terra nullius" in 1095?

Is there a way to proportionalize fixed costs in a MILP?

Would Mirko Vosk, Mind Drinker trigger Waste Not?

Global BGP Routing only by only importing supernet prefixes

"Mouth-breathing" as slang for stupidity

Escape Velocity - Won't the orbital path just become larger with higher initial velocity?

When did Bilbo and Frodo learn that Gandalf was a Maia?

Transition to "Starvation Mode" in Survival Situations

How far did Gandalf and the Balrog drop from the bridge in Moria?

Is there a fallacy about "appeal to 'big words'"?

Telephone number in spoken words

Swap (and hibernation) on SSD in 2019?

What would it take to get a message to another star?

Why did IBM make the PC BIOS source code public?

What unique challenges/limitations will I face if I start a career as a pilot at 45 years old?

Dogfights in outer space

How to not forget things?

Word for an event that will likely never happen again



Letting unbanned users comment


opening a text file and building a dictionaryIterating and fetching values from dictionaryLoop an array of dictionary with keys containg sets; comparing each key, value pair; and combining dictionariesSettings JAVA_HOME with PowershellSimple Stack DemoHomogeneousDict - The Python dictionary that can have homogeneous keysRSA algorithm implementation in Python 3Search father (x) of the number D(x) and measure time and memory consumed






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








5












$begingroup$


I am a beginner in python and I was wondering if the code for this program is nice and clean. The program works I was wondering how could i improve it. So this program asks for your 'username' and checks to see if your 'username' is in the 'banned users' list. If it is then it will 'log out' and won't let you comment. If your 'username' isn't in the 'banned uses' list it will let you comment and ask if you would like to let someone else to comment. If you enter 'yes' the 'while loop' will run again if there isn't then the program will print the username and comments. This is basically what the program does. How can I improve it?



# We define a function here.
def display_comments():
"""Displays User Input (comments)"""
if user_comments: # If the dictionary 'user_comments' has at least one key value pair then execute this block of code.
print("nTop Comments:")
for username, comment in user_comments.items(): # Define a for loop for each key value pair in the dictionary 'user_comments'.
print(f"ntusername: comment.") # Print each key value pair.

username_prompt = "nUsername: "
comment_prompt = "Comment: "
continue_prompt = "nWould you like to let another user comment (yes/no)? "

banned_users = ['pete', 'jack', 'ali', 'henry', 'jason', 'emily'] # List of banned users.
user_comments = # Empty dictionary that we will store user input later on in the program.

account_active = True # Set up a flag

while account_active: # Run as long as the 'account_active' flag remains 'True'.
username = input(username_prompt) # Ask for a username and store it in the variable 'username'.

if username.lower() in banned_users: # Cross reference the username with the banned_users list. If the username is in the banned_users list then execute the following code.
print(f"nI'm sorry username but you are banned from using this site.") # Tell the user that they are banned.
print("Deactivating...") # Simulate logging out.

account_active = False # Set the flag to 'False'

else: # Run this code only if the user is not in the 'banned_users' list.
comment = input(comment_prompt) # Ask the user for their comment and store it in the variable 'comment'.

user_comments[username] = comment # Add the username and comment (key-value pair) to the 'user_comments' dictionary.

repeat = input(continue_prompt) # Ask the user if they want to repeat and store it in the variable 'repeat'.

if repeat == 'no': # If the variable 'repeat' has the value 'no' then execute the following code, otherwise run the while loop again.
account_active = False # Set the flag to 'False'

display_comments() # Call the 'display_comments()' function to print the key-value pairs in the dictionary.









share|improve this question









New contributor



snow_razer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$









  • 4




    $begingroup$
    Usually you would put a summary of the problem or what the code does in the title. The desire to improve the code is implied by posting to this site.
    $endgroup$
    – spyr03
    8 hours ago










  • $begingroup$
    spyr03 Thanks bro, I changed it.
    $endgroup$
    – snow_razer
    8 hours ago


















5












$begingroup$


I am a beginner in python and I was wondering if the code for this program is nice and clean. The program works I was wondering how could i improve it. So this program asks for your 'username' and checks to see if your 'username' is in the 'banned users' list. If it is then it will 'log out' and won't let you comment. If your 'username' isn't in the 'banned uses' list it will let you comment and ask if you would like to let someone else to comment. If you enter 'yes' the 'while loop' will run again if there isn't then the program will print the username and comments. This is basically what the program does. How can I improve it?



# We define a function here.
def display_comments():
"""Displays User Input (comments)"""
if user_comments: # If the dictionary 'user_comments' has at least one key value pair then execute this block of code.
print("nTop Comments:")
for username, comment in user_comments.items(): # Define a for loop for each key value pair in the dictionary 'user_comments'.
print(f"ntusername: comment.") # Print each key value pair.

username_prompt = "nUsername: "
comment_prompt = "Comment: "
continue_prompt = "nWould you like to let another user comment (yes/no)? "

banned_users = ['pete', 'jack', 'ali', 'henry', 'jason', 'emily'] # List of banned users.
user_comments = # Empty dictionary that we will store user input later on in the program.

account_active = True # Set up a flag

while account_active: # Run as long as the 'account_active' flag remains 'True'.
username = input(username_prompt) # Ask for a username and store it in the variable 'username'.

if username.lower() in banned_users: # Cross reference the username with the banned_users list. If the username is in the banned_users list then execute the following code.
print(f"nI'm sorry username but you are banned from using this site.") # Tell the user that they are banned.
print("Deactivating...") # Simulate logging out.

account_active = False # Set the flag to 'False'

else: # Run this code only if the user is not in the 'banned_users' list.
comment = input(comment_prompt) # Ask the user for their comment and store it in the variable 'comment'.

user_comments[username] = comment # Add the username and comment (key-value pair) to the 'user_comments' dictionary.

repeat = input(continue_prompt) # Ask the user if they want to repeat and store it in the variable 'repeat'.

if repeat == 'no': # If the variable 'repeat' has the value 'no' then execute the following code, otherwise run the while loop again.
account_active = False # Set the flag to 'False'

display_comments() # Call the 'display_comments()' function to print the key-value pairs in the dictionary.









share|improve this question









New contributor



snow_razer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$









  • 4




    $begingroup$
    Usually you would put a summary of the problem or what the code does in the title. The desire to improve the code is implied by posting to this site.
    $endgroup$
    – spyr03
    8 hours ago










  • $begingroup$
    spyr03 Thanks bro, I changed it.
    $endgroup$
    – snow_razer
    8 hours ago














5












5








5


1



$begingroup$


I am a beginner in python and I was wondering if the code for this program is nice and clean. The program works I was wondering how could i improve it. So this program asks for your 'username' and checks to see if your 'username' is in the 'banned users' list. If it is then it will 'log out' and won't let you comment. If your 'username' isn't in the 'banned uses' list it will let you comment and ask if you would like to let someone else to comment. If you enter 'yes' the 'while loop' will run again if there isn't then the program will print the username and comments. This is basically what the program does. How can I improve it?



# We define a function here.
def display_comments():
"""Displays User Input (comments)"""
if user_comments: # If the dictionary 'user_comments' has at least one key value pair then execute this block of code.
print("nTop Comments:")
for username, comment in user_comments.items(): # Define a for loop for each key value pair in the dictionary 'user_comments'.
print(f"ntusername: comment.") # Print each key value pair.

username_prompt = "nUsername: "
comment_prompt = "Comment: "
continue_prompt = "nWould you like to let another user comment (yes/no)? "

banned_users = ['pete', 'jack', 'ali', 'henry', 'jason', 'emily'] # List of banned users.
user_comments = # Empty dictionary that we will store user input later on in the program.

account_active = True # Set up a flag

while account_active: # Run as long as the 'account_active' flag remains 'True'.
username = input(username_prompt) # Ask for a username and store it in the variable 'username'.

if username.lower() in banned_users: # Cross reference the username with the banned_users list. If the username is in the banned_users list then execute the following code.
print(f"nI'm sorry username but you are banned from using this site.") # Tell the user that they are banned.
print("Deactivating...") # Simulate logging out.

account_active = False # Set the flag to 'False'

else: # Run this code only if the user is not in the 'banned_users' list.
comment = input(comment_prompt) # Ask the user for their comment and store it in the variable 'comment'.

user_comments[username] = comment # Add the username and comment (key-value pair) to the 'user_comments' dictionary.

repeat = input(continue_prompt) # Ask the user if they want to repeat and store it in the variable 'repeat'.

if repeat == 'no': # If the variable 'repeat' has the value 'no' then execute the following code, otherwise run the while loop again.
account_active = False # Set the flag to 'False'

display_comments() # Call the 'display_comments()' function to print the key-value pairs in the dictionary.









share|improve this question









New contributor



snow_razer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$




I am a beginner in python and I was wondering if the code for this program is nice and clean. The program works I was wondering how could i improve it. So this program asks for your 'username' and checks to see if your 'username' is in the 'banned users' list. If it is then it will 'log out' and won't let you comment. If your 'username' isn't in the 'banned uses' list it will let you comment and ask if you would like to let someone else to comment. If you enter 'yes' the 'while loop' will run again if there isn't then the program will print the username and comments. This is basically what the program does. How can I improve it?



# We define a function here.
def display_comments():
"""Displays User Input (comments)"""
if user_comments: # If the dictionary 'user_comments' has at least one key value pair then execute this block of code.
print("nTop Comments:")
for username, comment in user_comments.items(): # Define a for loop for each key value pair in the dictionary 'user_comments'.
print(f"ntusername: comment.") # Print each key value pair.

username_prompt = "nUsername: "
comment_prompt = "Comment: "
continue_prompt = "nWould you like to let another user comment (yes/no)? "

banned_users = ['pete', 'jack', 'ali', 'henry', 'jason', 'emily'] # List of banned users.
user_comments = # Empty dictionary that we will store user input later on in the program.

account_active = True # Set up a flag

while account_active: # Run as long as the 'account_active' flag remains 'True'.
username = input(username_prompt) # Ask for a username and store it in the variable 'username'.

if username.lower() in banned_users: # Cross reference the username with the banned_users list. If the username is in the banned_users list then execute the following code.
print(f"nI'm sorry username but you are banned from using this site.") # Tell the user that they are banned.
print("Deactivating...") # Simulate logging out.

account_active = False # Set the flag to 'False'

else: # Run this code only if the user is not in the 'banned_users' list.
comment = input(comment_prompt) # Ask the user for their comment and store it in the variable 'comment'.

user_comments[username] = comment # Add the username and comment (key-value pair) to the 'user_comments' dictionary.

repeat = input(continue_prompt) # Ask the user if they want to repeat and store it in the variable 'repeat'.

if repeat == 'no': # If the variable 'repeat' has the value 'no' then execute the following code, otherwise run the while loop again.
account_active = False # Set the flag to 'False'

display_comments() # Call the 'display_comments()' function to print the key-value pairs in the dictionary.






python beginner






share|improve this question









New contributor



snow_razer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



snow_razer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 8 hours ago









dfhwze

8,5211 gold badge18 silver badges50 bronze badges




8,5211 gold badge18 silver badges50 bronze badges






New contributor



snow_razer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 8 hours ago









snow_razersnow_razer

314 bronze badges




314 bronze badges




New contributor



snow_razer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




snow_razer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 4




    $begingroup$
    Usually you would put a summary of the problem or what the code does in the title. The desire to improve the code is implied by posting to this site.
    $endgroup$
    – spyr03
    8 hours ago










  • $begingroup$
    spyr03 Thanks bro, I changed it.
    $endgroup$
    – snow_razer
    8 hours ago













  • 4




    $begingroup$
    Usually you would put a summary of the problem or what the code does in the title. The desire to improve the code is implied by posting to this site.
    $endgroup$
    – spyr03
    8 hours ago










  • $begingroup$
    spyr03 Thanks bro, I changed it.
    $endgroup$
    – snow_razer
    8 hours ago








4




4




$begingroup$
Usually you would put a summary of the problem or what the code does in the title. The desire to improve the code is implied by posting to this site.
$endgroup$
– spyr03
8 hours ago




$begingroup$
Usually you would put a summary of the problem or what the code does in the title. The desire to improve the code is implied by posting to this site.
$endgroup$
– spyr03
8 hours ago












$begingroup$
spyr03 Thanks bro, I changed it.
$endgroup$
– snow_razer
8 hours ago





$begingroup$
spyr03 Thanks bro, I changed it.
$endgroup$
– snow_razer
8 hours ago











1 Answer
1






active

oldest

votes


















5












$begingroup$

The code is pretty clear in what it does, good work.




# We define a function here.
def ...
...
... = # Empty dictionary
...
... = False # Set the flag to 'False'


Many of these comments are not all that useful. I can see by looking at the code that the flag is set to false, or that a function has been defined. Usually you would comment with the reason why a piece of code exists. If you removed a comment from beside some code, could you still work out what the code does? If so the comment is probably not useful.




def display_comments():
"""Displays User Input (comments)"""
if user_comments: # If the dictionary 'user_comments' has at least one key value pair then execute this block of code.
print("nTop Comments:")
for username, comment in user_comments.items(): # Define a for loop for each key value pair in the dictionary 'user_comments'.
print(f"ntusername: comment.") # Print each key value pair.


This looks pretty good. It does what it says it does. Remove the inline comments and it is golden.




banned_users = ['pete', 'jack', ...
...
if username.lower() in banned_users:


Extracting banned users to a list is good. An issue might arise here if a with an uppercase letter is is added to the banned list. He won't be prevented from commenting! In general there are two ways to avoid this problem



  1. Do a case-insenstive compare. In a Java this would be done with username.equalsIgnoreCase(banned_user)

  2. Normalize both strings and then compare. This is the recommended method in Python.

So I would suggest making sure every banned user has been lowercased



banned_users = ['pete', 'jack', ...
banned_users = [banned_user.lower() for banned_user in banned_users]


If you believe you'll ever need to deal with international usernames casefold may be of interest. It will normalize strings more aggressively than lower will.



One result of lowercasing the names is that it cuts down on the number of available usernames. If Ben Dor and B. Endor both try to sign up as BenDor and BEndor respectively, only one can get the name. Is that OK? Will this ever be a problem?




repeat = input(continue_prompt)
if repeat == 'no':


This is one place I would lowercase as an answer of "NO" or "No" clearly indicate the user is done.




user_comments = 
...
user_comments[username] = comment


As the comments are stored in a dictionary, every username will only have one comment, the latest one they've made. For instance if the chat was meant to be



A: where is the coal?
B: walk west for 2 mins then
B: north for 1 min


The comments appear as



A: where is the coal?
B: north for 1 min


Is that intended? If not you can change to use a list containing the username and comment instead



comments = []
...
comments.append((username, comment))


and display_comments looks nearly identical



for username, comment in comments:
...





share|improve this answer









$endgroup$














  • $begingroup$
    You're explanation is very clear and understandable. I just didn't get one point that you made, and it's probably because I am a beginner. You said that I can change to using a list containing usernames and comments instead, you wrote: 'comments.append((username, comment))'.Could you please explain how this works since I thought that the elements in the list had no correlation with each other unlike key-value pairs in dictionaries.
    $endgroup$
    – snow_razer
    7 hours ago







  • 1




    $begingroup$
    You are right that elements in a list are separate things. However if you look at the elements each one is itself a tuple of the username and comment. So in the same way you can have a list of points [(1, 2), (-3, 5), (x1, y1)] you have a list of username/comment pairs [('pete', 'hello'), ('jack', 'hi there'), ('pete', 'are you nearby?')]
    $endgroup$
    – spyr03
    7 hours ago






  • 1




    $begingroup$
    notice the extra pair of brackets in comments.append(). Append takes one object to add to the list, so I want to add the tuple (username, comment) to the list
    $endgroup$
    – spyr03
    7 hours ago










  • $begingroup$
    I fully understood, thank you very much. One last question though, how would the for loop work to extract the tuples from the list and print it.
    $endgroup$
    – snow_razer
    7 hours ago






  • 1




    $begingroup$
    The keyword to search is tuple unpacking. If first_pair = comments[0] you can unpack the pair with username, comment = first_pair. Since for x in y assigns to x each object in y, you can unpack in the loop header. Thus for pair in comments becomes for username, comment in comments.
    $endgroup$
    – spyr03
    6 hours ago














Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






snow_razer is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f226120%2fletting-unbanned-users-comment%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5












$begingroup$

The code is pretty clear in what it does, good work.




# We define a function here.
def ...
...
... = # Empty dictionary
...
... = False # Set the flag to 'False'


Many of these comments are not all that useful. I can see by looking at the code that the flag is set to false, or that a function has been defined. Usually you would comment with the reason why a piece of code exists. If you removed a comment from beside some code, could you still work out what the code does? If so the comment is probably not useful.




def display_comments():
"""Displays User Input (comments)"""
if user_comments: # If the dictionary 'user_comments' has at least one key value pair then execute this block of code.
print("nTop Comments:")
for username, comment in user_comments.items(): # Define a for loop for each key value pair in the dictionary 'user_comments'.
print(f"ntusername: comment.") # Print each key value pair.


This looks pretty good. It does what it says it does. Remove the inline comments and it is golden.




banned_users = ['pete', 'jack', ...
...
if username.lower() in banned_users:


Extracting banned users to a list is good. An issue might arise here if a with an uppercase letter is is added to the banned list. He won't be prevented from commenting! In general there are two ways to avoid this problem



  1. Do a case-insenstive compare. In a Java this would be done with username.equalsIgnoreCase(banned_user)

  2. Normalize both strings and then compare. This is the recommended method in Python.

So I would suggest making sure every banned user has been lowercased



banned_users = ['pete', 'jack', ...
banned_users = [banned_user.lower() for banned_user in banned_users]


If you believe you'll ever need to deal with international usernames casefold may be of interest. It will normalize strings more aggressively than lower will.



One result of lowercasing the names is that it cuts down on the number of available usernames. If Ben Dor and B. Endor both try to sign up as BenDor and BEndor respectively, only one can get the name. Is that OK? Will this ever be a problem?




repeat = input(continue_prompt)
if repeat == 'no':


This is one place I would lowercase as an answer of "NO" or "No" clearly indicate the user is done.




user_comments = 
...
user_comments[username] = comment


As the comments are stored in a dictionary, every username will only have one comment, the latest one they've made. For instance if the chat was meant to be



A: where is the coal?
B: walk west for 2 mins then
B: north for 1 min


The comments appear as



A: where is the coal?
B: north for 1 min


Is that intended? If not you can change to use a list containing the username and comment instead



comments = []
...
comments.append((username, comment))


and display_comments looks nearly identical



for username, comment in comments:
...





share|improve this answer









$endgroup$














  • $begingroup$
    You're explanation is very clear and understandable. I just didn't get one point that you made, and it's probably because I am a beginner. You said that I can change to using a list containing usernames and comments instead, you wrote: 'comments.append((username, comment))'.Could you please explain how this works since I thought that the elements in the list had no correlation with each other unlike key-value pairs in dictionaries.
    $endgroup$
    – snow_razer
    7 hours ago







  • 1




    $begingroup$
    You are right that elements in a list are separate things. However if you look at the elements each one is itself a tuple of the username and comment. So in the same way you can have a list of points [(1, 2), (-3, 5), (x1, y1)] you have a list of username/comment pairs [('pete', 'hello'), ('jack', 'hi there'), ('pete', 'are you nearby?')]
    $endgroup$
    – spyr03
    7 hours ago






  • 1




    $begingroup$
    notice the extra pair of brackets in comments.append(). Append takes one object to add to the list, so I want to add the tuple (username, comment) to the list
    $endgroup$
    – spyr03
    7 hours ago










  • $begingroup$
    I fully understood, thank you very much. One last question though, how would the for loop work to extract the tuples from the list and print it.
    $endgroup$
    – snow_razer
    7 hours ago






  • 1




    $begingroup$
    The keyword to search is tuple unpacking. If first_pair = comments[0] you can unpack the pair with username, comment = first_pair. Since for x in y assigns to x each object in y, you can unpack in the loop header. Thus for pair in comments becomes for username, comment in comments.
    $endgroup$
    – spyr03
    6 hours ago
















5












$begingroup$

The code is pretty clear in what it does, good work.




# We define a function here.
def ...
...
... = # Empty dictionary
...
... = False # Set the flag to 'False'


Many of these comments are not all that useful. I can see by looking at the code that the flag is set to false, or that a function has been defined. Usually you would comment with the reason why a piece of code exists. If you removed a comment from beside some code, could you still work out what the code does? If so the comment is probably not useful.




def display_comments():
"""Displays User Input (comments)"""
if user_comments: # If the dictionary 'user_comments' has at least one key value pair then execute this block of code.
print("nTop Comments:")
for username, comment in user_comments.items(): # Define a for loop for each key value pair in the dictionary 'user_comments'.
print(f"ntusername: comment.") # Print each key value pair.


This looks pretty good. It does what it says it does. Remove the inline comments and it is golden.




banned_users = ['pete', 'jack', ...
...
if username.lower() in banned_users:


Extracting banned users to a list is good. An issue might arise here if a with an uppercase letter is is added to the banned list. He won't be prevented from commenting! In general there are two ways to avoid this problem



  1. Do a case-insenstive compare. In a Java this would be done with username.equalsIgnoreCase(banned_user)

  2. Normalize both strings and then compare. This is the recommended method in Python.

So I would suggest making sure every banned user has been lowercased



banned_users = ['pete', 'jack', ...
banned_users = [banned_user.lower() for banned_user in banned_users]


If you believe you'll ever need to deal with international usernames casefold may be of interest. It will normalize strings more aggressively than lower will.



One result of lowercasing the names is that it cuts down on the number of available usernames. If Ben Dor and B. Endor both try to sign up as BenDor and BEndor respectively, only one can get the name. Is that OK? Will this ever be a problem?




repeat = input(continue_prompt)
if repeat == 'no':


This is one place I would lowercase as an answer of "NO" or "No" clearly indicate the user is done.




user_comments = 
...
user_comments[username] = comment


As the comments are stored in a dictionary, every username will only have one comment, the latest one they've made. For instance if the chat was meant to be



A: where is the coal?
B: walk west for 2 mins then
B: north for 1 min


The comments appear as



A: where is the coal?
B: north for 1 min


Is that intended? If not you can change to use a list containing the username and comment instead



comments = []
...
comments.append((username, comment))


and display_comments looks nearly identical



for username, comment in comments:
...





share|improve this answer









$endgroup$














  • $begingroup$
    You're explanation is very clear and understandable. I just didn't get one point that you made, and it's probably because I am a beginner. You said that I can change to using a list containing usernames and comments instead, you wrote: 'comments.append((username, comment))'.Could you please explain how this works since I thought that the elements in the list had no correlation with each other unlike key-value pairs in dictionaries.
    $endgroup$
    – snow_razer
    7 hours ago







  • 1




    $begingroup$
    You are right that elements in a list are separate things. However if you look at the elements each one is itself a tuple of the username and comment. So in the same way you can have a list of points [(1, 2), (-3, 5), (x1, y1)] you have a list of username/comment pairs [('pete', 'hello'), ('jack', 'hi there'), ('pete', 'are you nearby?')]
    $endgroup$
    – spyr03
    7 hours ago






  • 1




    $begingroup$
    notice the extra pair of brackets in comments.append(). Append takes one object to add to the list, so I want to add the tuple (username, comment) to the list
    $endgroup$
    – spyr03
    7 hours ago










  • $begingroup$
    I fully understood, thank you very much. One last question though, how would the for loop work to extract the tuples from the list and print it.
    $endgroup$
    – snow_razer
    7 hours ago






  • 1




    $begingroup$
    The keyword to search is tuple unpacking. If first_pair = comments[0] you can unpack the pair with username, comment = first_pair. Since for x in y assigns to x each object in y, you can unpack in the loop header. Thus for pair in comments becomes for username, comment in comments.
    $endgroup$
    – spyr03
    6 hours ago














5












5








5





$begingroup$

The code is pretty clear in what it does, good work.




# We define a function here.
def ...
...
... = # Empty dictionary
...
... = False # Set the flag to 'False'


Many of these comments are not all that useful. I can see by looking at the code that the flag is set to false, or that a function has been defined. Usually you would comment with the reason why a piece of code exists. If you removed a comment from beside some code, could you still work out what the code does? If so the comment is probably not useful.




def display_comments():
"""Displays User Input (comments)"""
if user_comments: # If the dictionary 'user_comments' has at least one key value pair then execute this block of code.
print("nTop Comments:")
for username, comment in user_comments.items(): # Define a for loop for each key value pair in the dictionary 'user_comments'.
print(f"ntusername: comment.") # Print each key value pair.


This looks pretty good. It does what it says it does. Remove the inline comments and it is golden.




banned_users = ['pete', 'jack', ...
...
if username.lower() in banned_users:


Extracting banned users to a list is good. An issue might arise here if a with an uppercase letter is is added to the banned list. He won't be prevented from commenting! In general there are two ways to avoid this problem



  1. Do a case-insenstive compare. In a Java this would be done with username.equalsIgnoreCase(banned_user)

  2. Normalize both strings and then compare. This is the recommended method in Python.

So I would suggest making sure every banned user has been lowercased



banned_users = ['pete', 'jack', ...
banned_users = [banned_user.lower() for banned_user in banned_users]


If you believe you'll ever need to deal with international usernames casefold may be of interest. It will normalize strings more aggressively than lower will.



One result of lowercasing the names is that it cuts down on the number of available usernames. If Ben Dor and B. Endor both try to sign up as BenDor and BEndor respectively, only one can get the name. Is that OK? Will this ever be a problem?




repeat = input(continue_prompt)
if repeat == 'no':


This is one place I would lowercase as an answer of "NO" or "No" clearly indicate the user is done.




user_comments = 
...
user_comments[username] = comment


As the comments are stored in a dictionary, every username will only have one comment, the latest one they've made. For instance if the chat was meant to be



A: where is the coal?
B: walk west for 2 mins then
B: north for 1 min


The comments appear as



A: where is the coal?
B: north for 1 min


Is that intended? If not you can change to use a list containing the username and comment instead



comments = []
...
comments.append((username, comment))


and display_comments looks nearly identical



for username, comment in comments:
...





share|improve this answer









$endgroup$



The code is pretty clear in what it does, good work.




# We define a function here.
def ...
...
... = # Empty dictionary
...
... = False # Set the flag to 'False'


Many of these comments are not all that useful. I can see by looking at the code that the flag is set to false, or that a function has been defined. Usually you would comment with the reason why a piece of code exists. If you removed a comment from beside some code, could you still work out what the code does? If so the comment is probably not useful.




def display_comments():
"""Displays User Input (comments)"""
if user_comments: # If the dictionary 'user_comments' has at least one key value pair then execute this block of code.
print("nTop Comments:")
for username, comment in user_comments.items(): # Define a for loop for each key value pair in the dictionary 'user_comments'.
print(f"ntusername: comment.") # Print each key value pair.


This looks pretty good. It does what it says it does. Remove the inline comments and it is golden.




banned_users = ['pete', 'jack', ...
...
if username.lower() in banned_users:


Extracting banned users to a list is good. An issue might arise here if a with an uppercase letter is is added to the banned list. He won't be prevented from commenting! In general there are two ways to avoid this problem



  1. Do a case-insenstive compare. In a Java this would be done with username.equalsIgnoreCase(banned_user)

  2. Normalize both strings and then compare. This is the recommended method in Python.

So I would suggest making sure every banned user has been lowercased



banned_users = ['pete', 'jack', ...
banned_users = [banned_user.lower() for banned_user in banned_users]


If you believe you'll ever need to deal with international usernames casefold may be of interest. It will normalize strings more aggressively than lower will.



One result of lowercasing the names is that it cuts down on the number of available usernames. If Ben Dor and B. Endor both try to sign up as BenDor and BEndor respectively, only one can get the name. Is that OK? Will this ever be a problem?




repeat = input(continue_prompt)
if repeat == 'no':


This is one place I would lowercase as an answer of "NO" or "No" clearly indicate the user is done.




user_comments = 
...
user_comments[username] = comment


As the comments are stored in a dictionary, every username will only have one comment, the latest one they've made. For instance if the chat was meant to be



A: where is the coal?
B: walk west for 2 mins then
B: north for 1 min


The comments appear as



A: where is the coal?
B: north for 1 min


Is that intended? If not you can change to use a list containing the username and comment instead



comments = []
...
comments.append((username, comment))


and display_comments looks nearly identical



for username, comment in comments:
...






share|improve this answer












share|improve this answer



share|improve this answer










answered 8 hours ago









spyr03spyr03

1,7705 silver badges20 bronze badges




1,7705 silver badges20 bronze badges














  • $begingroup$
    You're explanation is very clear and understandable. I just didn't get one point that you made, and it's probably because I am a beginner. You said that I can change to using a list containing usernames and comments instead, you wrote: 'comments.append((username, comment))'.Could you please explain how this works since I thought that the elements in the list had no correlation with each other unlike key-value pairs in dictionaries.
    $endgroup$
    – snow_razer
    7 hours ago







  • 1




    $begingroup$
    You are right that elements in a list are separate things. However if you look at the elements each one is itself a tuple of the username and comment. So in the same way you can have a list of points [(1, 2), (-3, 5), (x1, y1)] you have a list of username/comment pairs [('pete', 'hello'), ('jack', 'hi there'), ('pete', 'are you nearby?')]
    $endgroup$
    – spyr03
    7 hours ago






  • 1




    $begingroup$
    notice the extra pair of brackets in comments.append(). Append takes one object to add to the list, so I want to add the tuple (username, comment) to the list
    $endgroup$
    – spyr03
    7 hours ago










  • $begingroup$
    I fully understood, thank you very much. One last question though, how would the for loop work to extract the tuples from the list and print it.
    $endgroup$
    – snow_razer
    7 hours ago






  • 1




    $begingroup$
    The keyword to search is tuple unpacking. If first_pair = comments[0] you can unpack the pair with username, comment = first_pair. Since for x in y assigns to x each object in y, you can unpack in the loop header. Thus for pair in comments becomes for username, comment in comments.
    $endgroup$
    – spyr03
    6 hours ago

















  • $begingroup$
    You're explanation is very clear and understandable. I just didn't get one point that you made, and it's probably because I am a beginner. You said that I can change to using a list containing usernames and comments instead, you wrote: 'comments.append((username, comment))'.Could you please explain how this works since I thought that the elements in the list had no correlation with each other unlike key-value pairs in dictionaries.
    $endgroup$
    – snow_razer
    7 hours ago







  • 1




    $begingroup$
    You are right that elements in a list are separate things. However if you look at the elements each one is itself a tuple of the username and comment. So in the same way you can have a list of points [(1, 2), (-3, 5), (x1, y1)] you have a list of username/comment pairs [('pete', 'hello'), ('jack', 'hi there'), ('pete', 'are you nearby?')]
    $endgroup$
    – spyr03
    7 hours ago






  • 1




    $begingroup$
    notice the extra pair of brackets in comments.append(). Append takes one object to add to the list, so I want to add the tuple (username, comment) to the list
    $endgroup$
    – spyr03
    7 hours ago










  • $begingroup$
    I fully understood, thank you very much. One last question though, how would the for loop work to extract the tuples from the list and print it.
    $endgroup$
    – snow_razer
    7 hours ago






  • 1




    $begingroup$
    The keyword to search is tuple unpacking. If first_pair = comments[0] you can unpack the pair with username, comment = first_pair. Since for x in y assigns to x each object in y, you can unpack in the loop header. Thus for pair in comments becomes for username, comment in comments.
    $endgroup$
    – spyr03
    6 hours ago
















$begingroup$
You're explanation is very clear and understandable. I just didn't get one point that you made, and it's probably because I am a beginner. You said that I can change to using a list containing usernames and comments instead, you wrote: 'comments.append((username, comment))'.Could you please explain how this works since I thought that the elements in the list had no correlation with each other unlike key-value pairs in dictionaries.
$endgroup$
– snow_razer
7 hours ago





$begingroup$
You're explanation is very clear and understandable. I just didn't get one point that you made, and it's probably because I am a beginner. You said that I can change to using a list containing usernames and comments instead, you wrote: 'comments.append((username, comment))'.Could you please explain how this works since I thought that the elements in the list had no correlation with each other unlike key-value pairs in dictionaries.
$endgroup$
– snow_razer
7 hours ago





1




1




$begingroup$
You are right that elements in a list are separate things. However if you look at the elements each one is itself a tuple of the username and comment. So in the same way you can have a list of points [(1, 2), (-3, 5), (x1, y1)] you have a list of username/comment pairs [('pete', 'hello'), ('jack', 'hi there'), ('pete', 'are you nearby?')]
$endgroup$
– spyr03
7 hours ago




$begingroup$
You are right that elements in a list are separate things. However if you look at the elements each one is itself a tuple of the username and comment. So in the same way you can have a list of points [(1, 2), (-3, 5), (x1, y1)] you have a list of username/comment pairs [('pete', 'hello'), ('jack', 'hi there'), ('pete', 'are you nearby?')]
$endgroup$
– spyr03
7 hours ago




1




1




$begingroup$
notice the extra pair of brackets in comments.append(). Append takes one object to add to the list, so I want to add the tuple (username, comment) to the list
$endgroup$
– spyr03
7 hours ago




$begingroup$
notice the extra pair of brackets in comments.append(). Append takes one object to add to the list, so I want to add the tuple (username, comment) to the list
$endgroup$
– spyr03
7 hours ago












$begingroup$
I fully understood, thank you very much. One last question though, how would the for loop work to extract the tuples from the list and print it.
$endgroup$
– snow_razer
7 hours ago




$begingroup$
I fully understood, thank you very much. One last question though, how would the for loop work to extract the tuples from the list and print it.
$endgroup$
– snow_razer
7 hours ago




1




1




$begingroup$
The keyword to search is tuple unpacking. If first_pair = comments[0] you can unpack the pair with username, comment = first_pair. Since for x in y assigns to x each object in y, you can unpack in the loop header. Thus for pair in comments becomes for username, comment in comments.
$endgroup$
– spyr03
6 hours ago





$begingroup$
The keyword to search is tuple unpacking. If first_pair = comments[0] you can unpack the pair with username, comment = first_pair. Since for x in y assigns to x each object in y, you can unpack in the loop header. Thus for pair in comments becomes for username, comment in comments.
$endgroup$
– spyr03
6 hours ago











snow_razer is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















snow_razer is a new contributor. Be nice, and check out our Code of Conduct.












snow_razer is a new contributor. Be nice, and check out our Code of Conduct.











snow_razer is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f226120%2fletting-unbanned-users-comment%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480