我需要使用python打印钻石,输入长度。我已经让它检查宽度取决于它是奇数还是偶数,还确保输入的数字是正确的。但我在打印结果时遇到了问题。我得到的是:
length : 10
*
***
*****
*******
*********
*********
*******
*****
***
*
length = 10 #This line don't exist normally, its just to avoid the input and every check.
lengthTest =
谁能告诉我为什么当我运行打印语句时,下面显示的简单的"words“for循环和其他类似的循环(它们都是从Python.org复制的)不能在IDLE中打印?当在Python 2.7.12 shell中运行下面的print语句和其他测试语句时,该函数仅跳到下一个空缩进行,而不打印任何内容。(我尝试使用和不使用缩进和括号来运行打印行。)
>>> words = ['cat', 'window', 'defenestrate']
>>> for w in words:
print w, len(w)
下面的代码应该创建一个函数来计算和返回所有偶数之和,从0到传递的数字为10(包括在内),使用while循环。但是相反,它只是创造了一个无限的循环,我不知道为什么。
def function(lex):
zero = 0
while zero <= lex:
while lex % 2 == 0:
print(lex)
zero = zero + 1
while lex % 2 =! 0:
zero = zero + 1
return sum
打印(功能(10))
为什么当我输入用空格分隔的字符串时(在while循环之外),并且我试图在屏幕上打印它们时,只出现我输入的第一个字符串,而当我在while循环中(参见代码)时,它会逐个打印所有字符串? //this one prints the strings one by one
string current;
while (cin >> current) {
cout << current << " \n";
}
//this one only prints the first one the user types
str
我目前在uni学习Python课程,大概一周前才开始学习。在本练习中,我需要使用For循环打印' buffalo‘。我有以下几点:
a = "Buffalo"
for req in range(0,8,1):
print(a,end=" ")
这将导致8x Buffalo,但第2,4,5,6和8水牛应该是小写的。有人能告诉我怎么做吗?我在网上找不到它
我正在上大学的编程课,问题单上的练习之一是写这段代码:
number = int(input())
x = 0
y = 0
for n in range(number):
if n % 2 == 0:
x += n
else:
y += n
print(x)
print(y)
只使用一个"for“循环,而不使用"while”或"if“。
代码的目的是找出偶数和奇数之和从零到输入的数字,并将其打印到屏幕上。
请记住,此时我们不应该知道函数。
我已经尝试了很长一段时间了,如果不使用" if“语句来知道循环变量是
我之所以这样问,是因为我正试图在我的朋友频道中摆弄一个irc机器人,但无论如何都不能让它正常工作
基本上这个机器人是phenny,它有一个python解释器,我试图让它打印出字符串"linux open source linux open source“100次。
x = 9000; while x >1: x = x - 1; print "linux"
和
x=9000; while x>0: print 'linux'; x = x-1 .
到目前为止我已经尝试过了,我不确定如何才能在一行代码中完成所有这些。
我试图使用嵌套循环打印下面的图片(我应该使用for & while循环):
**
****
******
********
从5个空格开始,每一行减少2。从2颗星星开始,每条线增加2颗。以4行结束。
我的代码在第一行上工作,但它没有在剩下的3行上打印星星。有人能看到错误可能是什么吗?请不要给出答案,我只是需要帮助理解逻辑错误!
int m = 6;
int n = 0;
for(int l = 1; l < 5; l++){
while(m > 0){
System.out.print(&
我正在练习嵌套的for循环练习,并想做一个打印如下内容的程序: How many rows do you want?: 4
0
000
00000
0000000 到目前为止我的程序如下: #include <stdio.h>
int main(void) {
int x;
printf("How many rows do you want in the pyramid? (newlines included): ");
scanf("%d", &x);
for (int i = 1; i <
我已经学习Python大约4天了,我只是在处理我的第一个问题。
import random
number=random.randint(1,10)
count=1
guess= int(input("Enter your guess between 1 and 10 : "))
while number != guess:
count = count + 1
if guess == number:
print("That is my number !")
while guess < number:
guess = int(in
我实现了约翰·古塔格( John )在书的2.4章中的手指练习,这本书名为(用Python编写)。
任务是:
编写一个程序,要求用户输入10个整数,然后打印输入的最大奇数。如果没有输入奇数,则应打印一条大意如此的消息。
代码没有漏洞吗?如何避免重复同一行10次?
a = int(input('Enter the number: '))
b = int(input('Enter the number: '))
c = int(input('Enter the number: '))
d = int(input('Enter the numb
我是python和Jupyter Notebook的新手 我编写代码的目的是请求用户引入10个不同的整数。程序应该返回用户之前介绍的最高奇数。 我的代码如下: i=1
c=1
y=1
while i<=10:
c=int(input('Enter an integer number: '))
if c%2==0:
print('The number is even')
elif c> y
y=c
print('y')
i=i+1 我的
我是Python的初学者。我试图解决这个问题:“如果我们有一个包含<1000行的文件,如何只打印奇数行?”这是我的密码:
with open(r'C:\Users\Savina\Desktop\rosalind_ini5.txt')as f:
n=1
num_lines=sum(1 for line in f)
while n<num_lines:
if n/2!=0:
a=f.readlines()[n]
print(a)
break
我在学习C#中的易失性、联锁和锁,编写了下面的代码来打印奇数和偶数,而不使用任何这些同步结构,我运行了几次这个代码,并且总是给出正确的答案,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace microsoft
{
class th2_odd_event
{
static int num = 1;
当我输入不同数量的数字时,我的代码工作正常,但问题是当我输入相同数量的数字时,它不会显示在奇数和偶数部分。 我尝试在//获取奇数和偶数部分时操作它,但最终没有打印输出。我使用获得奇数的等式if (c[i] % 2 != 0)和获得偶数的if (c[i] % 2 == 0)。 import java.util.Scanner;
public class Sample {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int a[] = n
我正在编写一个连接到USB串行设备的python脚本。无论何时发送和执行命令,PIC都会返回一个hashtag。即。“命令执行成功。\n#”
我希望我的python脚本在输出数据之前等待hashtag。我该怎么做呢?这就是我所拥有的。它似乎并不实际打印从PIC收到的文本。任何帮助我们都将不胜感激
if port.isOpen():
try:
for x in range(0,100):
time.sleep(0.05)
port.write("command 1" + "\r\n")