Python Beginner: How do I print the name of a variable instead of the int that is connected to the variable?

huangapple go评论73阅读模式
英文:

Python Beginner: How do I print the name of a variable instead of the int that is connected to the variable?

问题

以下是您提供的Python代码的翻译部分:

初学者学Python为了一项作业我需要从用户那里获取工作日工作的整点数在获取到这些数据后我需要打印一个总结并基于用户输入的小时数显示消息告诉他们在某一天工作得太多或太少然而我理解因为我给了变量一个工作日的名称所以我得到的整数是一个数字因此当我连接它时它只打印数字而不是工作日的名称

如何改进代码以便消息将打印为星期一工作时间过长而不是9工作时间过长”。

不过我理解因为我给了变量一个工作日的名称所以我得到的整数是一个数字因此当我连接它时它只打印数字而不是工作日的名称

如何改进代码以便消息将打印为星期一工作时间过长而不是9工作时间过长”。

我知道在代码中我已经把一周的工作日排成一排所以它会打印出类似9工作时间过长等内容

我编写的代码如下

```python
week = input("输入当前工作周:")
id = input("输入员工ID:")
name = input("输入员工姓名:")
monday = int(input("输入星期一工作时间:"))
tuesday = int(input("输入星期二工作时间:"))
wednesday = int(input("输入星期三工作时间:"))
thursday = int(input("输入星期四工作时间:"))
friday = int(input("输入星期五工作时间:"))

print("员工总结:", id,)
for i in [monday, tuesday, wednesday, thursday, friday]:
    if i > 8:
        print("星期一工作时间过长")
    elif i < 4:
        print("星期一工作时间不足")

希望这有所帮助。如果您有任何其他问题,请随时提问。

英文:

Beginner in python here.
For an assignment I need to get input from the user for whole hours worked on weekdays.
After I get this, I need to print a summary and based on the hours that the user inputs, messages show up saying they worked too much or not enough on a given day.
However. I understand that because I have given the variable a weekday name, the integer I get is a number.
So when I link this it prints just the numbers and not the weekday name.

How can I improve the code so the message would print Too many hours worked on Monday instead of Too many hours worked on 9.

However. I understand that because I have given the variable a weekday name, the integer I get is a number.
So when I link this it prints just the numbers and not the weekday name.

How can I improve the code so the message would print Too many hours worked on Monday instead of Too many hours worked on 9.
I know in the code I have put the days of the week in a row so it prints Too many hours worked on 9 etc

The code I wrote is as follows:

week = input(&quot;Enter Current Working Week: &quot;)
id = input(&quot;Enter Employee ID: &quot;)
name = input(&quot;Enter Employee Name: &quot;)
monday = int(input(&quot;Enter hours worked for Monday: &quot;))
tuesday = int(input(&quot;Enter hours worked for Tuesday: &quot;))
wednesday = int(input(&quot;Enter hours worked for Wednesday: &quot;))
thursday = int(input(&quot;Enter hours worked for Thursday: &quot;))
friday = int(input(&quot;Enter hours worked for Friday: &quot;))

print(&quot;Summary for Employee&quot;, id,)
for i in monday, tuesday, wednesday, thursday, friday:
    if i &gt; 8:
        print(&quot;Too many hours worked on&quot;, monday, tuesday, wednesday, thursday, friday)
    elif i &lt; 4:
     print(&quot;Insufficient hours worked on&quot;, monday, tuesday, wednesday, thursday, friday)) 

答案1

得分: 0

以下是翻译好的部分:

# 星期几数字到名称的字典映射
weekday_names = {
    1: "星期一",
    2: "星期二",
    3: "星期三",
    4: "星期四",
    5: "星期五"
}

# 初始化一个空列表,用于存储每个工作日的工作小时数
hours_worked = []

# 获取用户输入的每个工作日的工作小时数
for i in range(1, 6):
    hours = int(input(f"输入 {weekday_names[i]} 工作的小时数:"))
    hours_worked.append(hours)

# 打印摘要并检查工作小时数
for i in range(5):
    if hours_worked[i] > 8:
        print(f"{weekday_names[i+1]} 工作时间太长。")
    elif hours_worked[i] < 8:
        print(f"{weekday_names[i+1]} 工作时间不够。")
    else:
        print(f"{weekday_names[i+1]} 工作了正好8小时。")

注意:我已将 HTML 实体编码(")还原为普通的双引号(")以便更好地理解代码。

英文:

Here is simple Example, you can modify whatever you needed.

# Dictionary mapping weekday numbers to names
weekday_names = {
    1: &quot;Monday&quot;,
    2: &quot;Tuesday&quot;,
    3: &quot;Wednesday&quot;,
    4: &quot;Thursday&quot;,
    5: &quot;Friday&quot;
}

# Initialize an empty list to store the hours worked for each weekday
hours_worked = []

# Get input from the user for each weekday
for i in range(1, 6):
    hours = int(input(f&quot;Enter the number of hours worked on {weekday_names[i]}: &quot;))
    hours_worked.append(hours)

# Print the summary and check for hours worked
for i in range(5):
    if hours_worked[i] &gt; 8:
        print(f&quot;Too many hours worked on {weekday_names[i+1]}.&quot;)
    elif hours_worked[i] &lt; 8:
        print(f&quot;Not enough hours worked on {weekday_names[i+1]}.&quot;)
    else:
        print(f&quot;Worked exactly 8 hours on {weekday_names[i+1]}.&quot;)

答案2

得分: 0

日历模块有一种方便的方式来获取星期几的名称。这意味着你可以像这样简化你的代码:

import calendar

days_worked = {}
hid = 8  # 一天中的小时数

# 创建一个以星期几名称为键的字典
for dow in calendar.day_name[:5]:
    days_worked[dow] = float(input(f'输入在 {dow} 工作的小时数:'))
# 分析字典
for k, v in days_worked.items():
    msg = '工作时间过多' if v > hid else '工作时间不足' if v < hid else f'工作时间恰好 {hid} 小时'
    print(f'{k} 工作了 {msg} 小时')
英文:

The calendar module has a convenient means of getting the names of the days of the week. This means that you can simplify your code like this:

import calendar

days_worked = {}
hid = 8 # hours in a day

# build a dictionary keyed by day names
for dow in calendar.day_name[:5]:
    days_worked[dow] = float(input(f&#39;Enter the number of hours worked on {dow}: &#39;))
# analyse the dictionary
for k, v in days_worked.items():
    msg = &#39;Too many&#39; if v &gt; hid else &#39;Too few&#39; if v &lt; hid else f&#39;Exactly {hid}&#39;
    print(f&#39;{msg} hours worked on {k}&#39;)

答案3

得分: 0

week = input("输入当前工作周:")
id = input("输入员工编号:")
name = input("输入员工姓名:")

用于存储每天工作小时数的字典

hours = {}

循环获取每天的工作小时数

for day in ['星期一', '星期二', '星期三', '星期四', '星期五']:
hours[day] = int(input("输入" + day + "的工作小时数:"))

print("员工摘要", id)
for day in hours:
if hours[day] > 8:
print(day + "工作小时数过多")
elif hours[day] < 4:
print(day + "工作小时数不足")

英文:
week = input(&quot;Enter Current Working Week: &quot;)
id = input(&quot;Enter Employee ID: &quot;)
name = input(&quot;Enter Employee Name: &quot;)
# dictionary to store hour worked for each day
hours = {}
# loop to get hours worked for each day
for day in [&#39;Monday&#39;, &#39;Tuesday&#39;, &#39;Wednesday&#39;, &#39;Thursday&#39;, &#39;Friday&#39;]:
hours[day] = int(input(&quot;Enter hours worked for &quot; + day + &quot;: &quot;))

print(&quot;Summary for Employee&quot;, id,)
for day in hours:
    if hours[day] &gt; 8:
       print(&quot;Too many hours worked on&quot;, day)
    elif hours[day] &lt; 4:
        print(&quot;Insufficient hours worked on&quot;, day)

I think this will help you understand it more easily on how to use dictionaries

答案4

得分: -1

问题在于你直接打印了工作的小时数,而没有打印这些小时对应的日期。你可以通过在字典中存储日期和它们对应的工作小时数,然后对它们进行迭代来解决这个问题。

以下是你可以修改的代码:

week = input("输入当前工作周:")
id = input("输入员工ID:")
name = input("输入员工姓名:")

# 创建一个字典来存储日期和工作小时数
work_hours = {
    "星期一": int(input("输入星期一的工作小时数:")),
    "星期二": int(input("输入星期二的工作小时数:")),
    "星期三": int(input("输入星期三的工作小时数:")),
    "星期四": int(input("输入星期四的工作小时数:")),
    "星期五": int(input("输入星期五的工作小时数:"))
}

print("员工", id, "的工作摘要")
for day, hours in work_hours.items():
    if hours > 8:
        print(day, "工作小时数过多")
    elif hours < 4:
        print(day, "工作小时数不足")

这样修改后,你的代码会打印出每天的日期,以及对应的工作小时数。

英文:

The issue is that you're printing the hours worked directly, instead of printing the day those hours correspond to. You can solve this problem by storing the days and their corresponding hours in a dictionary, and then iterating over that.

Here's how you can modify your code:

week = input(&quot;Enter Current Working Week: &quot;)
id = input(&quot;Enter Employee ID: &quot;)
name = input(&quot;Enter Employee Name: &quot;)

# Create a dictionary to store days and hours
work_hours = {
    &quot;Monday&quot;: int(input(&quot;Enter hours worked for Monday: &quot;)),
    &quot;Tuesday&quot;: int(input(&quot;Enter hours worked for Tuesday: &quot;)),
    &quot;Wednesday&quot;: int(input(&quot;Enter hours worked for Wednesday: &quot;)),
    &quot;Thursday&quot;: int(input(&quot;Enter hours worked for Thursday: &quot;)),
    &quot;Friday&quot;: int(input(&quot;Enter hours worked for Friday: &quot;))
}

print(&quot;Summary for Employee&quot;, id,)
for day, hours in work_hours.items():
    if hours &gt; 8:
        print(&quot;Too many hours worked on&quot;, day)
    elif hours &lt; 4:
        print(&quot;Insufficient hours worked on&quot;, day)

huangapple
  • 本文由 发表于 2023年6月6日 14:36:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定