Version 3 of Python will be breaking backwards compatibility with 2.x. So learning the newer features the language has to offer and focusing on being forward compatible is of more importance to myself.
Yeah, so wouldn’t that mean that the code you offered would be useless?
New features aren’t going to be how the new Python works, its going to be syntax mostly that breaks the compatibility, from what I understand. So, using a new feature doesn’t ensure compatibility at all.
Besides, at least for the 2.x range, a normal if statement would be backwards AND forwards compatible across all the versions.
The new features have been added are all part of the plan for 3.x and the older, slower stuff that they make redundant is getting removed.
Of course you can be both forward and backwards in this case (and most of the time). I just don’t think it’s worth the bloated code or time time worrying about, since 2.5 has been out for quite some time now and 3.x coming along so soon.
Yes, I’m sure they’re going to remove if statements.
I just don’t understand how to use it, and it only accepts 3 arguments. So, if I needed to add things, then I couldn’t. So, I would STILL like an explanation for how it works, and an example of it written as an if statement.
I assumed Myke’s post was sufficient.
It’s like a if statement but backwards, you give a value that should be returned then a condition and then a value to return if the condition is false. Please use google if you need more help as it’s already well documented (and pretty self explanatory IMO).
print "true value" if "condition" else "false value"
As a if statement…
if "condition":
print "true value"
else:
print "false value"
Actually you can use multiple ternary expressions if required.
apples = 3
print "an apple" if apples <= 1 else "a couple of apples" if apples == 2 else "a few apples" if apples == 3 else "a bunch of apples"
As a if statement (which in this case is much easier to read)…
apples = 3
if apples <= 1:
print "an apple"
if apples == 2:
print "a couple of apples"
if apples == 3:
print "a few apples"
else:
print "a bunch of apples"
I see.
I’m trying to port the program to C (as I’m trying to learn it now), and I can’t figure out how to split the user’s input’s digits into different cells of an array.
For instance, if I type in 456, I want it to go to: (4)(5)(6)
I can’t figure it out. And the C program that was posted in the begining of this thread isn’t helpful, because conio.h doesn’t seem to be mentioned anywhere in the C Library Reference Guide.
Edit: I feel kinda stupid, as conio.h didn’t have much to do with the overall program it seems. Also, found this on Yahoo Answers.
Been a long time since I’ve posted here, but I got a C program for this working!
#include <stdio.h>
int n, t;
int get();
int check( int );
int main()
{
int g;
do {
g = get();
check( g );
} while ( t == 0 );
getchar();
getchar();
}
int get()
{
int g;
printf( "Please insert a number: " );
scanf( "%d", &g );
n = g;
return g;
}
int check( g )
{
int num = g, sum = 0, digit;
while ( num != 0 ) {
digit = num % 10;
sum += digit*digit;
num /= 10;
}
if ( sum == 1 ) {
printf( "%d is happy!
", n );
t = 1;
} else if ( sum == 42 || sum == g ) {
printf( "%d is not happy.
", n );
t = 0;
} else {
check( sum );
}
}
It’s ready to compile.
Edit: Now it keeps asking you until you give it a happy number.
For those without compilers:
http://bellard.org/tcc/
To use: (windows)
Run using CMD tcc.exe yourfile.c
Or if you want something serious try eclipse. (but it’s 60 MB)
I just use MinGW personally.
Edit: The code was modified. Now it keeps asking you until you choose a happy number.
I’ve modified the code further, and turned it into using long ints and short ints where appropriate. Should be more memory efficient now.
#include <stdio.h>
long int n;
int t;
long int get();
short int check();
int main()
{
long int guess;
do {
guess = get();
check( guess );
} while ( t == 0 );
getchar();
getchar();
}
long int get()
{
long int guess;
printf( "Please insert a number: " );
scanf( "%ld", &guess );
n = guess;
return guess;
}
short int check( guess )
{
long int tmp = guess;
int sum = 0;
short int digit;
while ( tmp != 0 ) {
digit = tmp % 10;
sum += digit*digit;
tmp /= 10;
}
if ( sum == 1 ) {
printf( "%ld is happy!
", n );
t = 1;
} else if ( sum == 42 || sum == n ) {
printf( "%ld is not happy.
", n );
t = 0;
} else {
check( sum );
}
}
Ok, now I have it so that no function calls itself, and also where I can tell it to check another number after I get a happy one:
#include <stdio.h>
long long int orig;
short int rep;
long long int get();
void check();
int main()
{
long long int guess;
char cont;
do {
do {
guess = get();
check( guess );
} while ( rep == 1 );
printf( "Would you like to check another number (y/n)?
" );
getchar();
scanf( "%c", &cont );
printf( "
" );
if ( cont == 'y' ) {
cont = 1;
} else if ( cont == 'n' ) {
cont = 0;
} else {
printf( "Invalid input, quitting by default.
" );
cont = 0;
}
} while ( cont == 1 );
}
long long int get()
{
long long int guess;
printf( "Please insert a number: " );
scanf( "%lld", &guess );
orig = guess;
return guess;
}
void check( guess )
{
long long int tmp = guess;
long long int sum;
short int digit;
short int loop;
do {
sum = 0;
while ( tmp != 0 ) {
digit = tmp % 10;
sum += digit * digit;
tmp /= 10;
}
if ( sum == 1 ) {
printf( "%lld is happy!
", orig );
rep = 0;
loop = 0;
} else if ( sum == 42 || sum == orig ) {
printf( "%lld is not happy.
", orig );
rep = 1;
loop = 0;
} else {
loop = 1;
tmp = sum;
}
} while ( loop == 1 );
}
However, I want to solve the problem with going nuts when I type an invalid number in.
How do I check for valid inputs?