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

Help me with my game!

Wed May 31, 2017 3:50 am

In my game.. When I type in "3" as the code, and then type in "3" as the answer... It says "try again", which is the opposite of what is supposed to happen as I typed in 3 and 3.

print("Hello, welcome to THE C0D3, please read rules and directions before playing")
print("Hello, boss... Welcome back, please type in a new code")
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)

ian_g
Posts: 36
Joined: Fri May 26, 2017 1:04 am
Location: East Midlands, UK

Re: Help me with my game!

Wed May 31, 2017 4:11 am

ever heard of a loop?

should I be regretful? I generally post code within an hour for a users' problem if I am sarky but with this I just think ...

Too young ? Too stupid? Too lazy?

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

Re: Help me with my game!

Wed May 31, 2017 4:32 am

Instead of saying that, can you actually help?...

ian_g
Posts: 36
Joined: Fri May 26, 2017 1:04 am
Location: East Midlands, UK

Re: Help me with my game!

Wed May 31, 2017 5:12 am

Why should I?

Quick check of the code and you don't know a loop or an if statement so why bother?

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

Re: Help me with my game!

Wed May 31, 2017 5:16 am

So I can actually learn about loops or if statements and fix this? I am indeed quite lazy.

ian_g
Posts: 36
Joined: Fri May 26, 2017 1:04 am
Location: East Midlands, UK

Re: Help me with my game!

Wed May 31, 2017 5:19 am

use code quote thingies and i'll see

ian_g
Posts: 36
Joined: Fri May 26, 2017 1:04 am
Location: East Midlands, UK

Re: Help me with my game!

Wed May 31, 2017 5:22 am

note how long that takes you

laziness is an integral part of a programmer It's also why we comment our code.

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

Re: Help me with my game!

Wed May 31, 2017 8:27 am

I'm feeling generous this morning and won't report that this topic is a duplicate of one that you didn't respond to despite getting help from others.

When you enter the code the first time, you convert it to an integer
When the 'player' try to guess a number, you do not convert it to an integer and the input remains a string
An integer like 9 will never equal equal a string of '9'.

Suggest you remove the `int` from the line of code where you enter the code.

PS another plea! please learn how to use code tags.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

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

Re: Help me with my game!

Wed May 31, 2017 8:34 am

@beamskeleton: Laziness expressed as taking steps to avoid needless repetition is admirable; laziness that shows itself through ignoring previous advice and expecting others to solve problems is not.

From previous replies:
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.
(scotty101)
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.
(paddyg)

Your current problem concerning the comparison of '3' has also been dealt with if you take the trouble to read what has been posted for you. It is a really basic feature of most programming languages that the internal representation of the number with value three is not the same as the character or string that will be displayed as the human-readable symbol three.

(As I have been typing that one of your previously ignored helpers has repeated the same advice...)

User avatar
RST8
Posts: 64
Joined: Tue Nov 25, 2014 1:57 pm

Re: Help me with my game!

Wed May 31, 2017 9:54 am

It's not always helpful to just solve someone's question, but to get you going and working on the basis that to do anything with this you're going to have to figure it out anyway, this is one approach, based on what I think you are trying to achieve, where a boss sets a master code and then a player has to guess that code. It needs additional work to make it friendlier on handling invalid inputs (such as letters) and allow a get out so you can quit the loops. It's not very "pythonic", but one step at a time.

This allows 4 guesses.

Code: Select all

# By setting code to an out of range number at the start
# we can save having an input outside and inside the loop
# but this is not always desirable, it's a lazy approach :-)

code = 1000
while (code > 999 or code <0 ):
    code =int (input("Enter master code: "))

print ("Master code =",code)

# Ok now wait till user cracks code or we run out of guesses
# Again, by setting answer to something that is incorrect
# we can avoid having two input statements.  

answer =-1
maxTries = 4 # Set this to the max number of guesses
currentTry = 0

while (answer  !=  code and currentTry<maxTries):
    answer = int (input("Guess the code: "))
    if (answer != code):
        print ("Sorry, not the correct code!")
        currentTry = currentTry +1

if currentTry == maxTries:
    print ("No more guesses left.")
else:
    if answer == code:
        print ("Guess was correct!")

Joe

Return to “Python”