INT5051 Python

Java Python Assignment 2
INT2067/INT5051 Introduction to Programming and Problem Solving
2024-2025 Semester 2
Due Date: March 16, 2025 (Sunday)
1 Introduction
In this assignment, you need to implement a text-based game based on the riddle about a
farmer who needs to cross a river with a fox, a goose, and a sack of corn.
2 Riddle
A farmer with a fox, a goose, and a sack of corn needs to cross a river. The farmer has a
rowboat, but there is room for only the farmer and one of her three items. Unfortunately,
both the fox and the goose are hungry. The fox cannot be left alone with the goose, or the
fox will eat the goose. Likewise, the goose cannot be left alone with the sack of corn, or
the goose will eat the corn. How does the farmer get everything across the river?
3 Game
In your text-based game, the farmer can move at most one item to the other shore each
time. You should ask the user to enter a command to determine what item the farmer
wants to take at each turn. The game ends when the user quits the game or when the user
has succeeded or failed.
4 Basic Design
You may follow the following basic flow in your program:
• Show item locations.
• Repeat:
1) Ask for the next command.
2) Update item locations if necessary.
3) Show item locations.
4) Exit the loop if the game has succeeded or failed.
You may need to add other steps and/or flows to fulfill all the requirements.
5 Requirement (70%)
Your program should do the following:
• [5%] Print the goal of the game at the start.
• [10%] Ask the user to enter a command at each turn, and continue until the user
requests to quit or the goal has succeeded or failed.
• [10%] Show the location of the boat and the three items (fox, goose, corn) clearly
at the start of the game and after each turn. You may follow the output in the
sample sections.
• Accept the following five one-character strings as valid commands: f, g, c, m or
q. After receiving a valid command, the game should:
o [10%] f, g or c: move the fox, goose, or corn, if available, to the opposite
shore along with the boat.
o [5%] m: move the boat to the opposite shore without any item.
o [5%] q: quite the game.
• [10%] Check whether the requested item is on the same shore as the boat when
the game receives the command f, g or c. If not, it should prompt an error and
allow the user to enter a command again.
• [10%] Check whether the game has succeeded or failed after each turn. The game
has succeeded if all items have been moved to the far shore. The game has failed
if the fox has eaten the goose or the goose has eaten the corn. When the game has
succeeded, it should congratulate the user. When the game has failed, it should
tell the user why.
• [5%] Show an error if the user has entered an unrecognized command character
(i.e., a string other than ‘f’, ‘g’, ‘c’, ‘m’ or ‘q’).
6 Program Design and User Interface (30%)
• [10%] You should break down your program into functions with reasonable
length.
• [10%] You should use functions and/or data structures to reduce redundancy.
• [10%] The output should be neat and the required information should be shown
clearly. The text in the output should be correct and contains no spelling mistakes.
7 Submission Requirements
Your Python file should be named as assignment2.py. Submit only the Python file
assignment2.py through Moodle. Failure to submit the file properly will result in a
penalty ofdai 写INT5051、Python 5 to 10 marks.
See Moodle for the exact time of the deadline for submission. Late submission is will
induce penalty.
8 Hints
Please check the discussions forum on Moodle regularly for any hints.
9 Plagiarism Policy
The assignment should be done only by yourself. Every line of code should be written by
you. The use of any artificial intelligence (AI) tools is NOT allowed in this assignment.
Discussions on the assignment should be kept at a high-level only. You are suggested to
refer your classmates to the relevant notes or lab exercises if you want to help them.
You need to put down a declaration for having not used AI tools as comment at the
beginning of your program file. For example, your declaration may look like this:
# Declaration: this code is my own individual and original work without AI assistant.
If you have used AI tools or consulted the assignment with anyone, you should provide
an acknowledgment as comment at the beginning of your program file. Give the names
of all classmates that you have turned to. For example, your acknowledgement may look
like:
# Acknowledgement: I have asked Thomas, Gordon, and Emily for help.
# Acknowledgement: I have used AI tools (such as ChatGPT, gpt4o via POE, etc.) in
working on this assignment.
If plagiarism is found, both the copier and originator will get zero mark for the assignment.
Repeat offenders may result in a deduction of the course grade.
10 Sample Input & Output 1
Welcome
You have to bring the fox, the goose, and the corn across the river.
------------------------
Boat
------------------------
Fox Goose Corn
You are now at the near shore, what do you want to take? g
Goose
------------------------
Boat
------------------------
Fox Corn
You are now at the far shore, what do you want to take? m
Goose
------------------------
Boat
------------------------
Fox Corn
You are now at the near shore, what do you want to take? f
Fox Goose
------------------------
Boat
------------------------
Corn
You are now at the far shore, what do you want to take? g
Fox
------------------------
Boat
------------------------
Goose Corn
You are now at the near shore, what do you want to take? c
Fox Corn
------------------------
Boat
------------------------
Goose
You are now at the far shore, what do you want to take? m
Fox Corn
------------------------
Boat
------------------------
Goose
You are now at the near shore, what do you want to take? g
Fox Goose Corn
------------------------
Boat
------------------------
Congratulations!
You have brought all items across the river.
11 Sample Input & Output 2
Welcome
You have to bring the fox, the goose, and the corn across the river.
------------------------
Boat
------------------------
Fox Goose Corn
You are now at the near shore, what do you want to take? goose
You have entered an invalid command.
------------------------
Boat
------------------------
Fox Goose Corn
You are now at the near shore, what do you want to take? f
Fox
------------------------
Boat
------------------------
Goose Corn
You have failed.
The goose has eaten the corn.
12 Reference
V. Anton Sprual. Think Like a Programmer: An Introduction to Creative Problem Solving.
No Starch Press, San Francisco, 2012         

### Python 中 `int` 函数的使用说明 #### 基本概念 在 Python 编程中,`int()` 是一个内置函数,主要用于将字符串或数字转换为整型。该函数支持多种参数配置,能够满足不同的需求[^2]。 #### 函数语法 以下是 `int()` 的两种主要形式: 1. **无参调用** 当不传递任何参数时,`int()` 默认返回值为 0。 ```python result = int() print(result) # 输出: 0 ``` 2. **带参数调用** - 参数 `x`: 可以为数字或符合数值格式的字符串。 - 参数 `base`: 表示输入数字的进制,默认为 10(十进制)。范围可以是从 0 到 36,其中 0 自动检测进制[^4]。 #### 转换规则 - 如果 `x` 是浮点数,则 `int()` 将其向下取整并忽略小数部分[^2]。 ```python float_value = 7.9 integer_value = int(float_value) print(integer_value) # 输出: 7 ``` - 如果 `x` 是字符串,必须能解析为有效的整数。否则会抛出 `ValueError` 错误[^3]。 ```python valid_string = '42' converted_integer = int(valid_string) print(converted_integer) # 输出: 42 invalid_string = 'abc' try: int(invalid_string) except ValueError as e: print(e) # 输出: invalid literal for int() with base 10: 'abc' ``` - 对于带有指定进制的情况,`int()` 支持将其他进制的字符串转换为十进制整数[^4]。 ```python binary_string = '1010' decimal_from_binary = int(binary_string, 2) print(decimal_from_binary) # 输出: 10 hexadecimal_string = 'ff' decimal_from_hexadecimal = int(hexadecimal_string, 16) print(decimal_from_hexadecimal) # 输出: 255 ``` #### 注意事项 - 若尝试将无法识别的字符串(如包含非法字符)作为参数传递给 `int()`,则会触发 `ValueError` 异常[^3]。 - 浮点数到整数的转换总是采用截断方式,即仅保留整数部分而舍去小数位[^2]。 --- ### 示例代码总结 以下是一些综合性的实例展示: ```python # 基础案例 print(int()) # 输出: 0 print(int(8.2)) # 输出: 8 print(int('-12')) # 输出: -12 # 不同进制转换 print(int('101', 2)) # 输出: 5 (二进制转十进制) print(int('f', 16)) # 输出: 15 (十六进制转十进制) # 处理异常情况 try: print(int('hello')) except ValueError as error_message: print(f"捕获错误: {error_message}") # 捕获错误: invalid literal for int() with base 10: 'hello' ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值