Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why is it crushing ?
#11
Here a example how you can modify (String Interpolation) the text:


# depending on your example
# print("You currently have, " + common + "common fishs, " + uncommon + "uncommon fishs, " + garbage + "garbage fishs")

# example variables:
common_fish = 12
uncommon_fish = 14
garbage_fish = 55

# string literal with named placeholders for the format method
quantity_text = "You currently have {common} common fishs, {uncommon} uncommon fishs and {garbage} garbage fishs"
quantity_output = quantity_text.format(common=common_fish, uncommon=uncommon_fish, garbage=garbage_fish) # Keyword Arguments
print(quantity_output)

# string literal with placeholders for the format method
quantity_text = "You currently have {} common fishs, {} uncommon fishs and {} garbage fishs"
quantity_output = quantity_text.format(common_fish, uncommon_fish, garbage_fish) # Arguments
# without names it's shorter, but when the formatting happens at another place, far away from the string
# literal, you should better use names
print(quantity_output)

# one-liner
# but it's too long
print("You currently have {} common fishs, {} uncommon fishs and {} garbage fishs".format(common_fish, uncommon_fish, garbage_fish))


# new super handy Python 3.6 format-strings
# recognize the f in front of the string literal
print(f"You currently have {common_fish} common fishs, {uncommon_fish} uncommon fishs and {garbage_fish} garbage fishs")
# it uses local variables

# another not often used method with a dict
quantity_text = "You currently have {common} common fishs, {uncommon} uncommon fishs and {garbage} garbage fishs"
# far away from this definition of the string literal somewhere in your program....
amount = {
    'common': 12,
    'uncommon': 14,
    'garbage': 55,
    }
print(quantity_text.format_map(amount))
The format method does the conversion for you. It tries first to get the string of the object with the magic __str__ method.
If the method does not exist, it tries the __repr__ method,
which should give back a representation of the object like this for example: <__main__.Foo object at 0x7f22ec6e7da0>

Avoid the use of + operator with strings, if you have different data types and/or having more than one variable and string literal.

If you need more control about formatting, you have to use the format method.
A very small example:

text = 'The pin is {:.1f} mm long'
length = 1.1934567891034
print(text.format(length))
For further information you should bookmark this page: https://pyformat.info/

Finally: Try to avoid the usage of global. Use arguments/keyword-arguments in your function definition:

def start():
    common = 0
    uncommon = 0
    garbage = 0
    coins = 100
    # your code........

    # here is the loop shifted from function begin into
    # this function
    while True:
        common, uncommon, garbage, coins = begin(common, uncommon, garbage, coins)
        # here the function begin is called with the arguments
        # the function does it's work
        # then the function returns the new values of the variables
        # First the function is evaluated, then the returned tupel is
        # assigned to the values
        # on the left side if more then one variable
        # this is called argument unpacking
        #
        # Break-Condition at the end
        # if the question is answered with Y/y, it breaks out of the loop
        # lower().strip() changes the input into lowercase and strip
        # removes white spaces at the beginning and the end
        # then it compares the equality to 'y' 
        if input('Do you want to quit (y/n): '.lower().strip() == 'y'):
            break

def begin(common, uncommon, garbage, coins):
    # code which prints text
    # and changes the values, which are
    # assigned to the names inside the function definition
    # there is no global needed
    #
    # don't call the function inside the function itself
    # it's recursion and the depth is limited to 3000 on my platform
    # 
    # return the changed variables
    return common, uncommon, garbage, coins
I think this demonstrates how to do it only with functions. Later you can use classes,
but first you should learn the use of return statement and arguments and/or keyword-arguments in functions.

Another important and very difficult thing for my opinion is choosing the right descriptive names for something.
Names for variables should be adjectives, for functions verbs and later for classes nouns.
You followed this rule, but the names are not descriptive.

The name begin is for example not very good. But for now I don't have a better idea. Maybe do_round?
For start you can just name it to: play_game or do_game_loop. A class can later named Game for example. But as mentioned before,
learn not yet about classes. I think this is too early.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

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