我刚开始学习 ruby,目前坚持在同一个脚本中使用 ARGV 和 gets.chomp。
我希望脚本首先解压 3 个参数,然后我会问一个问题 (gets.chomp),然后打印包含 ARGV 和 gets.chomp 变量之一的字符串。在终端中,我将 ARGV 设置为一二三(例如:ruby file1.rb 一二三)。下面的代码示例:
first, second, third = ARGV
puts "Your first variable is: #{first}"
puts "Your second variable is: #{second}"
puts "Your third variable is: #{third}"
Run Code Online (Sandbox Code Playgroud)
这完全符合我的预期。在终端中,它给了我一、二和三作为第一、第二和第三个变量。
我在 puts 中添加了“你最喜欢的颜色是什么”,这会按预期打印出来,但是当我为输入设置 gets.chomp 时,出现错误。
first, second, third = ARGV
puts "Your first variable is: #{first}"
puts "Your second variable is: #{second}"
puts "Your third variable is: #{third}"
puts "What is your favourite colour? "
colour = …Run Code Online (Sandbox Code Playgroud) 我正在创建一个网站,并成功地使用外部 CSS 文件为移动和桌面样式创建了媒体查询。您可以看到下面链接到外部工作表的 HTML;mobile.css 用于<767px 的设备,desktop.css 用于>768px 的设备。
<head>
<meta name="viewpoint" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<link rel="stylesheet" media="screen and (max-width: 767px)" href="css/mobile.css" />
<link rel="stylesheet" media="screen and (min-width: 768px)" href="css/desktop.css" />
</head>
Run Code Online (Sandbox Code Playgroud)
设计了这个网站后,我相信我可以让它更具响应性,并希望将 desktop.css 更改为 tablet.css 并为超过 1000px 的设备创建一个新的 desktop.css。但是,将以下外部 CSS 与媒体查询链接起来不起作用:
<head>
<meta name="viewpoint" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<link rel="stylesheet" media="screen and (max-width: 767px)" href="css/mobile.css" /> <!-- This works fine -->
<link rel="stylesheet" media="screen and (min-width: 768px)" href="css/tablet.css" /> <!-- This works fine -->
<link rel="stylesheet" media="screen and (min-width: …Run Code Online (Sandbox Code Playgroud) 我正在学习ruby并且在使用%Modulus总和时遇到问题.
puts "example #{101 % 4}"
Run Code Online (Sandbox Code Playgroud)
上面打印1在终端,这是我所期望的.
为什么下面打印101在终端?当然它和上面一样吗?
puts "example #{100 + 1 % 4}"
Run Code Online (Sandbox Code Playgroud)
我知道%只是说"X除以Y而J剩余"的另一种方式.所以第二个例子肯定也应该返回1?
任何帮助将不胜感激.
我在使用简单的Python脚本时遇到了麻烦.该脚本有一个温度列表输入,我想打印0到5之外的所有温度.
这工作正常,直到我输入浮点.例如,如果列表具有1,4,6,-2,则它仅按预期打印6和-2.如果我输入1,4,4.3,6,则打印出4.3和6.
我知道这个问题与浮点有关,并且出于某种原因,如果没有语句通过浮点数.虽然我确信这很简单,但我已经搜索了高低,但没有运气.
你知道为什么会这样吗?
# input for temperatures
temperatures = [1, 4, 4.3,6]
# empty output list
output_list = []
for temperature in temperatures:
if temperature not in range (0,6):
output_list = output_list + [temperature]
# print the output_list
print(output_list)
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助.