Python大学在线课程作业求助:伪代码及流程图制作指导
Hey Carl! As someone who’s been where you are—staring at confusing course materials and wondering if your code hits the mark—let’s walk through this together. First off, great job putting together code that checks all the boxes for your assignment!
Your code absolutely meets the assignment requirements:
- Strings: All user inputs (
pet,dog_name,cat_name) and sound values ("woof", "meow") are string types. - Function: You defined the reusable
animal()function that accepts parameters and executes a set of actions. - Loop: The
for letter in nameloop iterates through each character in the pet’s name to print the sound repeatedly.
A tiny optional tweak: If you want to make input case-insensitive (so users can type "Dog" or "CAT" and it still works), modify the condition checks like this:
if pet.lower() == "dog": # rest of the code elif pet.lower() == "cat": # rest of the code
Pseudocode is plain-language step-by-step logic—no strict syntax needed, just clarity. Here’s how to write it for your code, plus general tips:
Example Pseudocode for Your Project
Start Ask user: "Do you have a dog or a cat?" → store response in variable pet Define function animal(name, sound): Print "[name] is saying hi!" For each character in name: Print sound + " " (without starting a new line) If pet equals "dog": Ask user: "What is your dog's name?" → store in dog_name Call animal(dog_name, "woof") Else if pet equals "cat": Ask user: "What is your cat's name?" → store in cat_name Call animal(cat_name, "meow") Else: Print "Try again :(" End
General Pseudocode Rules
- Use simple verbs (Ask, Define, Print, Loop, If) to describe actions
- Follow the exact order your code runs in
- Don’t stress about punctuation or programming syntax—focus on making the logic easy to follow
Flowcharts use basic symbols to map out your code’s logic flow. Here’s the breakdown for your project, plus standard symbols to use:
Standard Flowchart Symbols
- Oval: Start/End point
- Rectangle: Action (input, print, function call)
- Diamond: Decision point (yes/no, if/else checks)
- Arrow: Shows the direction of the flow
Step-by-Step Flowchart for Your Code
- Oval (Start)
- Rectangle: Display "Do you have a dog or a cat?" → get user input → save to
pet - Diamond: Is
petequal to "dog"?- Yes → Rectangle: Display "What is your dog's name?" → get input → save to
dog_name→ Rectangle: Callanimal(dog_name, "woof")→ jump to step 8 - No → Diamond: Is
petequal to "cat"?- Yes → Rectangle: Display "What is your cat's name?" → get input → save to
cat_name→ Rectangle: Callanimal(cat_name, "meow")→ jump to step 8 - No → Rectangle: Print "Try again :(" → jump to step 8
- Yes → Rectangle: Display "What is your cat's name?" → get input → save to
- Yes → Rectangle: Display "What is your dog's name?" → get input → save to
- (Sub-flow for
animalfunction)- Rectangle: Print "[name] is saying hi!"
- Diamond: Are there remaining characters in
name?- Yes → Rectangle: Print sound + " " → go back to the "remaining characters" diamond
- No → Exit sub-flow
- Oval (End)
You can draw this using free tools like Google Drawings or Miro—just drag and drop the symbols and connect them with arrows!
内容的提问来源于stack exchange,提问作者Carl32100

