简体   繁体   中英

Im a beginner and don't understand what's wrong with my code

So I am trying to make it that if a number is below 50, my code will print out Freestyle and if it is in the range of 51-100 that it will print breaststroke, and finally if it is above 101 that it will print out butterfly.

I am trying to get this and set it up for my swim mates and and for my coach to use to make practice more fun.

Edit I do not receive any output.

This is currently my code:

import random

def swimming():
    x = random.randint(1, 150)
    if x <= 50:
        print("Freestyle")
    elif x >= range(51, 100):
        print("Breaststroke")
    else:
        print("Butterfly")
import random

def swimming():
  x = random.randint(1, 150)
  if x < 51:
     print("Freestyle")
  elif x >51 and x<100:
     print("Breaststroke")
  else:
     print("Butterfly")
swimming()

This will give you output.

Since you did not post your own code, here's some quick code I whipped up that I think will do what you want:

def fun(x):

    if x < 51: # Below 51 (0-50)

        print("Freestyle")

    elif x > 100: # Above 100 (101+)

        print("Butterfly")

    else: # In between (51-100)

        print("Breaststroke")

Where x is a number that you put into the function as a parameter.

Your code only call for "def", which means "define" (or at least, my interpretation). To do that (ie, "running the script"), you also need to call it out in the end. Furthermore, range is like a list, not a specific 2 points, so calling >= range doesn't work.

Your code should look like this (minimum change)

import random

def swimming():
    x = random.randint(1, 150)
    if x <= 50:
        print("Freestyle")
    elif x in range(51, 100):
        print("Breaststroke")
    else:
        print("Butterfly")

swimming()

Note, at this point, it will return a random value, not specified by the user. If you actually want to input a value, check what style you want to swim, and loop back, it should be more like this

import random

def swimming():
    print('What is your number?')
    try:
        x = float(input())
        if x <= 50:
            print("Freestyle")
        elif x in range(51, 100):
            print("Breaststroke")
        else:
            print("Butterfly")
    except:
        print('Input a number please')


while True:
    answer = input('Do you want to continue? (Y/N)\n')
    if answer.lower() == 'y' or answer.lower() == 'yes':
        swimming()
    else:
        print('Thank you for playing.')
        input('Press enter to exit.')
        break

The "try" and "except" in the def block is to make sure that the input is a number (either an integer or a decimal). The "while True" loop is for "continuously working" (as long as you enter "y" or "yes" or "Y" or "YES"). If you don't like that, delete the whole chunk, and just keep "swimming()"

Keep up the good work.

def swimming():
    for i in range(5):
        x=random.randint(1,150)
        if x<=50:
            print("Freestyle")  
        elif x>=51 and x<=100:
            print("Breaststroke")
        else:
            print("Butterfly")

swimming()

you can use the first for loop to take the inputs as much as you want. i think it is more easy to use the logical operators with this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM