beamskeleton
Posts: 9
Joined: Mon May 22, 2017 4:13 am

How to prevent spamming on this game?

Thu May 25, 2017 4:28 pm

On my game, people can spam the codes and just break it.... How do I make it so spamming is prevented or isn't dangerous to my game?

print ("Welcome to THE C0D3, please read the rules & directions before playing this fun and........ VERY FRUSTRATING GAME.")
A = input('HEY BOSS, I FORGOT.. WHAT EXACTLY WHAT YOUR CODE NAME?: ')
print ("Hello, welcome back to the job, ", A)
while True:
question1 = int(input("ENTER A CODE BETWEEN 0 - 999, BOSS: "))
if 0 <= question1 <= 999:
break
print('ERROR, TRY AGAIN')
for i in range(300):
print(" ")
print ('Codebreaker, you must break the code')
answer = input ("Guess the code!: ")
if question1 == answer:
print ("Correct")
else:
B = input ("Try again!: ")
if question1 == B:
print ("Correct")
else:
C = input ("Try again!: ")
if question1 == C:
print ("Correct")
else:
D = input ("Try again!: ")
if question1 == D:
print ("Correct")
else:
E = input ("Try again!: ")
if question1 == E:
print ("Correct")
else:
G = input ("Try again!: ")
if question1 == G:
print ("Correct")
else:
H = input ("Try again!: ")
if question1 == H:
print ("Correct")
else:
I = input ("Try again!: ")
if question1 == I:
print ("Correct")
else:
J = input ("Try again!: ")
if question1 == J:
print ("Correct")
else:
L = input ("Try again!: ")
if question1 == L:
print ("Correct")
else:
M = input ("Try again!: ")
if question1 == M:
print ("Correct")
else:
N = input ("Try again!: ")
if question1 == N:
print ("Correct")
else:
P = input ("Try again!: ")
if question1 == P:
print ("Correct")
else:
Q = input ("Try again!: ")
if question1 == Q:
print ("Correct")
else:
R = input ("Try again!: ")
if question1 == R:
print ("Correct")
else:
S = input ("Try again!: ")
if question1 == S:
print ("Correct")
else:
W = input ("Try again!: ")
if question1 == W:
print ("Correct")
else:
X = input ("Try again!: ")
if question1 == X:
print ("Correct")
else:
Y = input ("Try again!: ")
if question1 == Y:
print ("Correct")
else:
Z = input ("Try again!: ")
if question1 == Z:
print ("Correct")
else:
DE = input ("Try again!: ")
if question1 == DE:
print ("Correct")
else:
FF = input ("Try again!: ")
if question1 == FF:
print ("Correct")
else:
EW = input ("Try again!: ")
if question1 == EW:
print ("Correct")
else:
EF = input ("Try again!: ")
if question1 == EF:
print ("Correct")
else:
PH = input ("Try again!: ")
if question1 == PH:
print ("Correct")
else:
AAA = input ("Try again!: ")
if question1 == AAA:
print ("Correct")
else:
BBB = input ("Try again!: ")
if question1 == BBB:
print ("Correct")
else:
CCC = input ("Try again!: ")
if question1 == CCC:
print ("Correct")
else:
DDD = input ("Try again!: ")
if question1 == DDD:
print ("Correct")
else:
EEE = input ("Try again!: ")
if question1 == EEE:
print ("Correct")
else:
FFF = input ("Try again!: ")
if question1 == FFF:
print ("Correct")
else:
GGG = input ("Try again!: ")
if question1 == GGG:
print ("Correct")
else:
HHH = input ("Try again!: ")
if question1 == HHH:
print ("Correct")
else:
III = input ("Try again!: ")
if question1 == III:
print ("Correct")
else:
JJJ = input ("Try again!: ")
if question1 == JJJ:
print ("Correct")
else:
LLL = input ("Try again!: ")
if question1 == LLL:
print ("Correct")
else:
MMM = input ("Try again!: ")
if question1 == MMM:
print ("Correct")
else:
NNN = input ("Try again!: ")
if question1 == NNN:
print ("Correct")
else:
PPP = input ("Try again!: ")
if question1 == PPP:
print ("Correct")
else:
QQQ = input ("Try again!: ")
if question1 == QQQ:
print ("Correct")
else:
RRR = input ("Try again!: ")
if question1 == SSS:
print ("Correct")
else:
TTT = input ("Try again!: ")
if question1 == TTT:
print ("Correct")
else:
XXE = input ("Try again!: ")
if question1 == XXE:
print ("Correct")
else:
YYY = input ("Try again!: ")
if question1 == YYY:
print ("Correct")
else:
ZZZ = input ("Try again!: ")
if question1 == ZZZ:
print ("Correct")
else:
print("ACCOUNT ON LOCKDOWN, THE CODE WAS ACTUALLY: " + question1)

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: How to prevent spamming on this game?

Thu May 25, 2017 4:40 pm

A. Define what you mean by spamming. What can the user do to 'spam' the game
B. Please please please use the code tags and place your code inside them. I can't tell what you are trying to do in your code without the correct indentation.

Code: Select all

print("This is inside code tags")
while True:
    print("Please use them whenever you post code")
C. I think you are talking about validating the entry from the user, and that you want to check that the entry is a number only. In which case, something like this should work. I'm using the .isdigit() method to check whether the user entered a string that only contains numbers.

Code: Select all

answer = 999

correct = False

#keep looping round until the user gets the answer correct
while correct == False:
    #Ask the user to enter a code
    question1 = input("Guess the code:  ")
    #Check if the answer is a number
    if question1.isdigit():
        #If it is a number, check to see if it is the correct answer
        if int(question1) == answer:
            #They got the answer right, tell them and exit the game
            print("Good job you are correct")
            correct = True
        else:
            #If it is not the correct answer, let the user know
            print("Nope, wrong answer")
    else:
        #The user must have entered something that wasn't a number, tell them off
        print("Play the game properly, numbers only!!")
Last edited by scotty101 on Thu May 25, 2017 4:55 pm, edited 1 time in total.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

beamskeleton
Posts: 9
Joined: Mon May 22, 2017 4:13 am

Re: How to prevent spamming on this game?

Thu May 25, 2017 4:45 pm

What I mean by "spamming" is that the player can just keep spamming keys... For example, you can spam "6" and then "enter".. If you do that, it'll break the game and exit.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to prevent spamming on this game?

Thu May 25, 2017 4:49 pm

beamskeleton wrote:What I mean by "spamming" is that the player can just keep spamming keys...
Recursion is a useful technique in Computer Science, but it isn't very useful when explaining things to human beings.

Could you try to explain without using the word 'spamming'?

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: How to prevent spamming on this game?

Thu May 25, 2017 4:51 pm

Why does it break the game? What error does it bring up?

To prevent users from breaking things we
a. Validate their entry. If we only want them to enter numbers then check that they've entered a number before doing anything with their entry (like I have done in my example, please try it and see what it does, if you are able to break it let me know and we'll work to fix it.)
b. Expect certain things to fail. If python raises an error, we can tell python to ignore an error of that type and tell it what to do if this happens. This is called exception handling.

You need to decide what you'd like the user to be able to enter and either check that they enter something valid or tell python what to do if it receives something invalid.

PS. I won't help you fix your code until you have reposted it inside code tags.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: How to prevent spamming on this game?

Thu May 25, 2017 9:52 pm

Also. The moment you find yourself typing almost the same thing more than once (or copying and pasting) you must stop and think how you can re-write your code using loops, functions, lists, classes etc. (This will help you enormously in avoiding bugs and fixing code). It doesn't look to me like you need different variable names and repeated code, just a try counter and one loop. And it's worth reading the python guidelines on code structure and variable, class, function naming conventions, these have been arrived at after thousands (millions?) of years of developer experience as the best way to make your code understandable (by you as well as others).

PS is the OP making a clever Monty Python reference?
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”