Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why is it crushing ?
#1
Hello guys i am totally new to python and programming at all
my first try was to make a simulator to the discord bot Tatsumaki fishing system but idk why is it keeps crushing
here is the code
import time
global coins
global common
global garbage
global uncommon
common = 0
uncommon = 0
garbage = 0
coins = 100
def start():
	print "Hello my friend!"
	name = raw_input("what's your name :")
	print "Welcome, "+ name + "!"
	print "This game is just like Tatsumaki Discord Bot fishing system, i hope that you enjoy it."
	print "the gole of the game is to collect as much fishs as possible then selling them for coins."
	print "Common fishs worth 12 coins/uncommon fishs worth 20 coins/garbage worth 8 coins."
	print "you have to pay 10 coins as a fee for fishing."

	choice = raw_input("Do you want to play Y/N ?:")
	if choice == "Y":
		print "To fish you have to use the command fish"
		begin()
	if choice == "N":
		print "Goodbye"
    	time.sleep(2)


def begin():
    global common
    global garbage
    global uncommon
    global coins  
    print "Let's start!"
    if coins > 500:
       print "You have won the game!"
       play = raw_input("Do you want to play again?:")
       if play == "Y":
          begin()
       if play == "N":
          print "Congrazts again"
          time.sleep(2)
    fish = raw_input("Do you want to fish ?:")
    if fish == "fish":
        print "You caught a common fish!"
        common=common+1
        coins=coins-10
        print "You currently have, "+common+"common fishs, "+uncommon+"uncommon fishs, "+garbage+"garbage fishs"
        print "your coins are now:"+coins
        begin()
    elif fish == "fish":
    	print "You caught an uncommon fish!"
        uncommon=uncommon+1
        coins=coins-10
        print "You currently have, "+common+"common fishs, "+uncommon+"uncommon fishs, "+garbage+"garbage fishs"
        print "your coins are now:"+coins
        begin()
    elif fish == "fish":
    	print "You caught a garbage fish!"
    	garbage=garbage+1
    	coins=coins-10
    	print "You currently have, "+common+"common fishs, "+uncommon+"uncommon fishs, "+garbage+"garbage fishs"
    	print "your coins are now:"+coins
    	begin()
    if fish == "N":
       sell = raw_input("Do you want to sell your fishs ? Y/N:")
       if sell == "Y":
          print "You currently have, "+common+"common fishs, "+uncommon+"uncommon fishs, "+garbage+"garbage fishs"
          print "You have sold your fishs."
          coins = common*12+uncommon*20+garbage*8
          common=0
          uncommon=0
          garbage=0
          print "your coins are now:"+coins
start()   		

it crushes when i get to this line
 fish = raw_input("Do you want to fish ?:")
i hope if you can help and thanks
note i am used sublime to write the code and github to launch it.
Reply
#2
Hello, please post full error traceback message in error code tags (red X icon next to Python code tags).

As an aside, it is highly recommended that you use Python 3 instead of P.2. Python 2 support is ending), Python 3 is the future.
Reply
#3
I modified your code to use in Python 3 and ran it. Namely raw_input() is input() in Python 3. And print is a function, not a statement, so you use it like print("This is printed!"). The code is:

import time

global coins
global common
global garbage
global uncommon
common = 0
uncommon = 0
garbage = 0
coins = 100


def start():
    print("Hello my friend!")
    name = input("what's your name :")
    print("Welcome, " + name + "!")
    print("This game is just like Tatsumaki Discord Bot fishing system, i hope that you enjoy it.")
    print("the gole of the game is to collect as much fishs as possible then selling them for coins.")
    print("Common fishs worth 12 coins/uncommon fishs worth 20 coins/garbage worth 8 coins.")
    print("you have to pay 10 coins as a fee for fishing.")

    choice = input("Do you want to play Y/N ?:")
    if choice == "Y":
        print("To fish you have to use the command fish")
        begin()
    if choice == "N":
        print("Goodbye")
        time.sleep(2)


def begin():
    global common
    global garbage
    global uncommon
    global coins
    print("Let's start!")
    if coins > 500:
        print("You have won the game!")
        play = input("Do you want to play again?:")
        if play == "Y":
            begin()
        if play == "N":
            print("Congrazts again")
            time.sleep(2)
    fish = input("Do you want to fish ?:")
    if fish == "fish":
        print("You caught a common fish!")
        common = common + 1
        coins = coins - 10
        print("You currently have, " + common + "common fishs, " + uncommon + "uncommon fishs, " + garbage + "garbage fishs")
        print("your coins are now:" + coins)
        begin()
    elif fish == "fish":
        print("You caught an uncommon fish!")
        uncommon = uncommon + 1
        coins = coins - 10
        print("You currently have, " + common + "common fishs, " + uncommon + "uncommon fishs, " + garbage + "garbage fishs")
        print("your coins are now:" + coins)
        begin()
    elif fish == "fish":
        print("You caught a garbage fish!")
        garbage = garbage + 1
        coins = coins - 10
        print("You currently have, " + common + "common fishs, " + uncommon + "uncommon fishs, " + garbage + "garbage fishs")
        print("your coins are now:" + coins)
        begin()
    if fish == "N":
        sell = input("Do you want to sell your fishs ? Y/N:")
        if sell == "Y":
            print("You currently have, " + common + "common fishs, " + uncommon + "uncommon fishs, " + garbage + "garbage fishs")
            print("You have sold your fishs.")
            coins = common * 12 + uncommon * 20 + garbage * 8
            common = 0
            uncommon = 0
            garbage = 0
            print("your coins are now:" + coins)


start()
I get an error with the print lines that print also variables, for example:
print("You currently have, " + common + "common fishs, " + uncommon + "uncommon fishs, " + garbage + "garbage fishs")
TypeError: Can't convert 'int' object to str implicitly
The variables you use in print are integers, while print can (at least directly) only take strings. So you need to convert them with str(), for example]:
str(common)
Reply
#4
(Jan-26-2018, 02:14 PM)j.crater Wrote: I modified your code to use in Python 3 and ran it. Namely raw_input() is input() in Python 3. And print is a function, not a statement, so you use it like print("This is printed!"). The code is:

import time

global coins
global common
global garbage
global uncommon
common = 0
uncommon = 0
garbage = 0
coins = 100


def start():
    print
    "Hello my friend!"
    name = input("what's your name :")
    print("Welcome, " + name + "!")
    print("This game is just like Tatsumaki Discord Bot fishing system, i hope that you enjoy it.")
    print("the gole of the game is to collect as much fishs as possible then selling them for coins.")
    print("Common fishs worth 12 coins/uncommon fishs worth 20 coins/garbage worth 8 coins.")
    print("you have to pay 10 coins as a fee for fishing.")

    choice = input("Do you want to play Y/N ?:")
    if choice == "Y":
        print("To fish you have to use the command fish")
        begin()
    if choice == "N":
        print("Goodbye")
        time.sleep(2)


def begin():
    global common
    global garbage
    global uncommon
    global coins
    print("Let's start!")
    if coins > 500:
        print("You have won the game!")
        play = input("Do you want to play again?:")
        if play == "Y":
            begin()
        if play == "N":
            print("Congrazts again")
            time.sleep(2)
    fish = input("Do you want to fish ?:")
    if fish == "fish":
        print("You caught a common fish!")
        common = common + 1
        coins = coins - 10
        print("You currently have, " + common + "common fishs, " + uncommon + "uncommon fishs, " + garbage + "garbage fishs")
        print("your coins are now:" + coins)
        begin()
    elif fish == "fish":
        print("You caught an uncommon fish!")
        uncommon = uncommon + 1
        coins = coins - 10
        print("You currently have, " + common + "common fishs, " + uncommon + "uncommon fishs, " + garbage + "garbage fishs")
        print("your coins are now:" + coins)
        begin()
    elif fish == "fish":
        print("You caught a garbage fish!")
        garbage = garbage + 1
        coins = coins - 10
        print("You currently have, " + common + "common fishs, " + uncommon + "uncommon fishs, " + garbage + "garbage fishs")
        print("your coins are now:" + coins)
        begin()
    if fish == "N":
        sell = input("Do you want to sell your fishs ? Y/N:")
        if sell == "Y":
            print("You currently have, " + common + "common fishs, " + uncommon + "uncommon fishs, " + garbage + "garbage fishs")
            print("You have sold your fishs.")
            coins = common * 12 + uncommon * 20 + garbage * 8
            common = 0
            uncommon = 0
            garbage = 0
            print("your coins are now:" + coins)


start()
I get an error with the print lines that print also variables, for example:
print("You currently have, " + common + "common fishs, " + uncommon + "uncommon fishs, " + garbage + "garbage fishs")
TypeError: Can't convert 'int' object to str implicitly
The variables you use in print are integers, while print can (at least directly) only take strings. So you need to convert them with [i]str()[/i, for example]:
str(common)

gonna install python 3 and about the error i am not getting any error it crushes when i tybe fish in the program
Reply
#5
Hmmm alright, that is strange. How are you running your code? I searched a bit and some people have had troubles with IDLE and raw_input.
Reply
#6
(Jan-26-2018, 02:25 PM)j.crater Wrote: Hmmm alright, that is strange. How are you running your code? I searched a bit and some people have had troubles with IDLE and raw_input.

i am running it like like that
from the command line
cd desktop then start python test.py

when i try to launch it from the IDLE i get this massage
Reply
#7
I've modified j.crater's code (the 'print' functions) so no conversion is necessary (still in Python 3)

Your other problem, is your initial "if" statement if fish == "fish": will always evaluate as true, so none of the "elif's" will be looked at. You could create a list of options, say choices = ['common', 'uncommon', 'garbage'] then randomly select one of those (Hint: Random), you could then have:
if choices == 'common':
    do something
elif choices == 'uncommon':
    do something
elif choices == 'garbage':
    do something
Another problem, is I can fish myself into debt as you have made no action to be taken if I reach 0 coins.

There are other smaller items that could be addressed, but I think you'll be fine if you handle the big problems first.

As to the error you are receiving, it's because in your code you've used both tabs and spaces for indentation. In Python, you should always use 4 spaces for indentation

And yes, by all means switch to Python 3 (3.6.4 if possible)
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#8
IDLE should have a Settings option under Edit (at the top) where you can set it to always insert spaces instead of tabs and set the number of spaces it inserts (the Python standard is 4 spaces).
Reply
#9
(Jan-26-2018, 03:59 PM)sparkz_alot Wrote: I've modified j.crater's code (the 'print' functions) so no conversion is necessary (still in Python 3)

Your other problem, is your initial "if" statement if fish == "fish": will always evaluate as true, so none of the "elif's" will be looked at. You could create a list of options, say choices = ['common', 'uncommon', 'garbage'] then randomly select one of those (Hint: Random), you could then have:
if choices == 'common':
    do something
elif choices == 'uncommon':
    do something
elif choices == 'garbage':
    do something
Another problem, is I can fish myself into debt as you have made no action to be taken if I reach 0 coins.

There are other smaller items that could be addressed, but I think you'll be fine if you handle the big problems first.

As to the error you are receiving, it's because in your code you've used both tabs and spaces for indentation. In Python, you should always use 4 spaces for indentation

And yes, by all means switch to Python 3 (3.6.4 if possible)
thanks very much it's working great now gonna do what you said about choice atm
Reply
#10
(Jan-26-2018, 03:59 PM)sparkz_alot Wrote: I've modified j.crater's code (the 'print' functions) so no conversion is necessary (still in Python 3)

Your other problem, is your initial "if" statement if fish == "fish": will always evaluate as true, so none of the "elif's" will be looked at. You could create a list of options, say choices = ['common', 'uncommon', 'garbage'] then randomly select one of those (Hint: Random), you could then have:
if choices == 'common':
    do something
elif choices == 'uncommon':
    do something
elif choices == 'garbage':
    do something
Another problem, is I can fish myself into debt as you have made no action to be taken if I reach 0 coins.

There are other smaller items that could be addressed, but I think you'll be fine if you handle the big problems first.

As to the error you are receiving, it's because in your code you've used both tabs and spaces for indentation. In Python, you should always use 4 spaces for indentation

And yes, by all means switch to Python 3 (3.6.4 if possible)

Curious: 1) why are the variables being declared as global twice? Wouldn't declaring them (common,coins,etc) global in in begin() suffice?

2) what's the benefit of ''.format(int) over concatenating str(int) as in the case of
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020