#๐Ÿ”’ [SUPER BEGINNER] Making a SUPER basic program to decide what outfit to wear with what weather.

47 messages ยท Page 1 of 1 (latest)

sand berry
#

Struggling to understand how to save the variables separately and I'm pulling my hair out trying to understand how to use these statements properly Im aware with how bad this looks so far but here is the horrid code halfway worked on im currently stuck on making the program recognize the weather_type and use it within the if-else statements if someone could tell me why my code looks so dumb, and also works dumb i would appreatiate it, I AM SUPER new so i appologize if it is the most basic fix but i have looked forever and i can't find what im looking for. Below is the disgusting code half finnished. Please forgive my in-experiance.

''' #Intitalize variables

temp, weater_type, wind speed.

temp = int(input("What is the temperature? (Fehrenheit only) "))

weather_type = str(input("What is the weather like? (Sunny, Rainy, Snowy) "))

rainy = "Rainy"
sunny = "Sunny"
snowy = "Snowy"
Rainy = "rainy"
Sunny = "sunny"
Snowy = "snowy"

wind_speed = int(input("What is the wind speed? (Knots only) "))

if temp <= 40 and wind_speed >=17:
print("Wear a coat and scarf, it's windy outside. ")

elif temp <= 40 :
print("Wear something warm, it's cold outside. ")

elif weather_type == "Snowy":
print("Wear a coat, scarf, and some mittens, it's a winter wonderland out there. ")

elif temp >= 40 and temp <= 70 and weather_type == "Sunny":
print("Wear a light jacket and some jeans, it's gonna be a nice day! ")

elif temp >= 70 and weather_type == "Sunny":
print("Wear a t-shirt and some shorts, it's a hot one! ")

elif temp <=90 and weather_type == "Rainy":
print("It is currently raining outside, wear a rain coat and some waterproof shoes. ")

elif temp >= 40 and temp <= 85:
print("Wear whatever feels nice, no bad weather in sight.")

else:
print("Program incomplete.")

input = (print("Press enter to continue.")) '''

waxen latchBOT
#

@sand berry

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

restive wing
# sand berry Struggling to understand how to save the variables separately and I'm pulling my...

Probably best to start with some questions and remarks:

This:

weather_type = str(input("What is the weather like? (Sunny, Rainy, Snowy) "))

The value form input() is always a string (str) so you can drop the str() surrounding it here.

What are these for?

rainy = "Rainy"
sunny = "Sunny"
snowy = "Snowy"
Rainy = "rainy"
Sunny = "sunny"
Snowy = "snowy"

They seem unused. If they're really unused, you could remove them and only bring them back if they become important later.

This final line:

input = (print("Press enter to continue.")) ]

Doesn't do what want. It:

  • print()s the message
  • assigns the return from print to the variable input
    Since I assume you can't to wait for someone to press enter, we'd normally write:
input("Press eneter to continue.")

which prompts, reads a response, and discards it.

#

!code

Also, edit your opening message to mark off the code as code.

waxen latchBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

sand berry
#

Originally i had placed the ''' rainy = "Rainy"
sunny = "Sunny"
snowy = "Snowy"
Rainy = "rainy"
Sunny = "sunny"
Snowy = "snowy" "' to try and save the program from reasding the wrong case, aka upper/lower

fickle moat
#

Your if-conditions can be improved a bit:

if temp <= 40:
  if wind_speed >= 17:
    print("Wear a coat and scarf, it's windy outside. ")
  else:
    print("Wear something warm, it's cold outside. ")

elif weather_type == "Snowy":
  print("Wear a coat, scarf, and some mittens, it's a winter wonderland out there. ")

elif weather_type == "Sunny":
  if 40 <= temp <= 70:
    print("Wear a light jacket and some jeans, it's gonna be a nice day! ")
  elif temp >= 70:
    print("Wear a t-shirt and some shorts, it's a hot one! ")

elif temp <= 90 and weather_type == "Rainy":
  print("It is currently raining outside, wear a rain coat and some waterproof shoes. ")

elif 40 <= temp <= 85:
  print("Wear whatever feels nice, no bad weather in sight.")
restive wing
fickle moat
#

Also I don't see a point to this last condition in the chain:

else: 
  print("Program incomplete.")
sand berry
sand berry
#

Also very grateful to all for the help here, and greatful for patience especially with how new i am im sure my questions seem horribly dumb/simple, so i am greatful for yalls time.

fickle moat
sand berry
#

Peach i just saw you if statement post ill fix mine up accordingly

#

I see now that the [and] does more damage than good here bless you i was struggling with those specifially.

fickle moat
#

Another thing you can do to improve your script is to check the user inputs for the input you expect. If the input isn't what you're expecting right now the script won't work properly. To avoid this, you can change your code before the if-statement chain to the following:

# Intitalize variables
# temp, weather_type, wind_speed

temp = input("What is the temperature in Fahrenheit?: ")

while not temp.isdigit():
  temp = input("What is the temperature in Fahrenheit?: ")

temp = int(temp)

weather_type = input("What is the weather like? [sunny/rainy/snowy]: ").lower()

while weather_type not in ["sunny", "rainy", "snowy"]:
  weather_type = input("What is the weather like? [sunny/rainy/snowy]: ").lower()

wind_speed = input("What is the wind speed in Knots?: ")

while not wind_speed.isdigit():
  wind_speed = input("What is the wind speed in Knots?: ")

wind_speed = int(wind_speed)
#

Do you understand while loops and what lists are?

#

If not I can explain further.

restive wing
sand berry
sand berry
restive wing
# sand berry That is what i was trying to do with the "' else: print("Program incomplete."...

That's possibly fine. PTE said it shouldn't fire I think?

The idea is that either:

  • no matches means do nothing, and that's fine
  • no matches means you missing something, and that's bad

For the former:

else:
    print("I have no advice! Enjoy the weather.")

For the latter:

else:
    raise ValueError(f'unhandled combination {weather_type=} and {temp=}')

which raises an exception which recites what the values were, to aid figuring out what went wrong.

#

But I always like an else: for long changes of choices, because they're so easy to either get wrong, or simply leave something out you forgot about.

fickle moat
# sand berry I've heard it several times and am struggling with them. I know that is ground l...

while loops evaluate a condition and if that condition evaluates to True the code indented in the while loop will run. In this case, after all the code indented in the while runs, the same condition is evaluated once again to see if it's equal to True. This process repeats until the condition evaluates to False. When the condition becomes False, the code inside the while does NOT run and instead execution of the script moves to after the while block and continues onwards from there

#

Lists are a collection data type in Python. They are used to store values of other data types, and can even include other lists or collections inside them. These stored values also don't have to be the same data type as each other. An empty list can be created by either calling list() and assigning it to a variable or or assigning [] to a variable (your choice). You can also create one-off lists on-the-fly if you're only going to use them in one location in the code, as I did with the modified user input checking code above (in that case the list was ["sunny", "rainy", "snowy"]). Checking for membership in a list is done with the in keyword. If I had the condition "test" in ["sunny", "rainy", "snowy"] it would evaluate to False as an example.

#

!e

vehicles = ["car", "truck", "airplane", "boat"]

print("spaceship" in vehicles)
print("car" in vehicles)
waxen latchBOT
fickle moat
#

Do you understand?

#

Also forgot to mention, you can remove and append values from a list as well:

vehicles = ["car", "truck", "airplane", "boat"]

vehicles.remove("car")
print(vehicles)
vehicles.append("spaceship")
print(vehicles)

The append method of list takes a value and add its to the END of the list. The remove method of a list takes a value and removes it from wherever it is in the list. It is often helpful to test if a value is in a list before attempting to remove it to avoid potential errors:

if "boat" in vehicles:
  vehicles.remove("boat")
#

Checking the length of a list using the len function can also be helpful:

if len(vehicles) < 2:
  print("You don't have many vehicles!")
#

Often you will also want to iterate over all the members of a list using a for-loop:

for v in vehicles:
  print(v)

When doing so v holds the value of each member of the vehicles list, one value per iteration of the for-loop. So the value of v changes for every iteration (it is re-assigned to the value of the next member of vehicles). So if vehicles was ["car", "truck", "airplane", "boat"], the first iteration of the for-loop would have v equal to "car", the next would have v equal to "truck", etc.

sand berry
# fickle moat Do you understand?

Alright, I have a few questions to clarify my understanding. First, are while loops used to check if the code has been read and understood? And does the code just randomly return different numbers? Was the Python bot's response a running of a while loop? And is that why some are true, and some are false? Is that used for troubleshooting or for navigating the list? or am i way off completely?

#

AHHHH I'm watching some in-depth while loop videos, and I SEE now!

fickle moat
# sand berry Alright, I have a few questions to clarify my understanding. First, are while lo...

No. Loops (including while loops) are used to repeat a section of code as long as a specific condition evaluates to True. The code does not randomly return different numbers. If you're talking about the Python bot's output above, you can ignore the numbers before the output. The real output of the program I gave to the bot above is:

False
True

The Python bot's response was running the code I posted just before it responded, but that code didn't include a while loop. It was this code: #1488295543515119668 message

The first output is False because the condition I told print to display ("spaceship" in vehicles) evaluates to False for the vehicles list I defined. The second condition is evaluated similarly. It is not used for navigating or troubleshooting the values in the list, it was just used as an example for your understanding of using the in keyword in Python.

sand berry
#

I understand it now. For example, in what I was trying, a while loop could be useful after each section to organize the code and prevent mistakes in each part. Like, right after I ask for the temp, could it ensure the individual entering the data would not proceed without entering a valid data type first?

#

I apologize for the slow responses. I'm actively trying to making sure I understand it before I move on.

fickle moat
sand berry
fickle moat
# sand berry Just ensuring that it is correct before proceeding? That's right, I remember tha...

Yes, well designed programs always prevent the input of invalid data by a user:

temp = input("What is the temperature in Fahrenheit?: ")

while not temp.isdigit():
  temp = input("What is the temperature in Fahrenheit?: ")

temp = int(temp)

In this case the script will keep prompting for input and reassigning to temp as long as you give an invalid response (where not temp.isdigit() will evaluate to True). After you get the valid input for temp which causes the while loop to be skipped or end, then you go ahead and convert temp to the int data type (last line) and move on.

sand berry
#

I almost left the int in again in the first line. I had to see you convert it back after that. That makes so much sense. And I was stressing almost every line to cram as much in as possible. This is way, way more organized.

restive wing
sand berry
#

Thank you both for the wisdom and your time. I have learned a lot, and I'm already absorbed in building this out! and motivated to write clean, readable code. Until next time, friends!

fickle moat
sand berry
#

!close

waxen latchBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.