## My code seemes right but doesn't work. Can anybody help?

### My code seemes right but doesn't work. Can anybody help?

I'm quite new to Python and I am pretty much self-studying. Right now I'm working on a game based on the board game Battleship. When the player's missile (or whatever it should be called) hits the computer's battleship (which means it's x and y position equals the battleship's x and y position) it should tell the player that it has hit the computer's battleship and then it does whatsoever. Same for the computer. But instead it says that the player missed, I know that because it prints out the x and y positions. If you know what went wrong you are most welcome to tell me. The code is underneath.
Code: Select all
`from random import randintfrom time import sleep, timeprint('Your battleship is roaming the seas, suddently another battleship is nearing you, and the battle starts.')man_hits = 0comp_hits = 0while man_hits < 2 and comp_hits < 2:    #Man shoot begin    battleshpbx = randint(1, 2)    battleshpby = randint(1, 2)    print(battleshpbx)    print(battleshpby)    shootax = input('Enter the x position of your missile. From 1 to 2.')    shootay = input('Enter the y position of your missile. From 1 to 2.')    if shootax == battleshpbx and shootay == battleshpby:        print('hit!')        man_hits = man_hits + 1    print(man_hits)    #Man shoot end    #Comp shoot start    battleshpax = input('Please enter the x position of your battleship. From 1 to 2.')    battleshpay = input('Please enter the y position of your battleship. From 1 to 2.')    shootbx = randint(1, 2)    shootby = randint(1, 2)    print(shootbx)    print(shootby)    if shootax == battleshpbx and shootay == battleshpby:        print('computer hit!')        comp_hits = comp_hits + 1    print(comp_hits)    #Comp shoot endif man_hits > comp_hits:    print('you win!')elif comp_hits > man_hits:    print('computer wins!')else:    print('it\'s a draw! try again.')`

Thank you!
Last edited by stranac on Sun May 15, 2016 10:19 am, edited 1 time in total.
Reason: First post lock.
tengjunkps

Posts: 1
Joined: Sat Jan 30, 2016 5:15 am

### Re: My code seemes right but doesn't work. Can anybody help?

In Python 3.x, input returns a string. In Python 2.x tries to evaluate that string before returning it. If you run your code in Python 3.x, shootax will be a string, and will not equal battleshpbx (which is an integer). To get it to work you need to use int() to covert the string from input to an integer. If you run your code in Python 2.x, input will convert numbers entered to integers, and it will work correctly.
Due to the reasons discussed here we will be moving to python-forum.io on October 1st, 2016.
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
ichabod801

Posts: 688
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC